Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Any PHP boffs know how to single-thread proggy?

Author
Message
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 27th Sep 2004 10:30
Doing an online highscore table for my retro-remakes compo game using PHP. Got NO database available and so storing table in a encrypted text file. There is a slim chance two or more people will upload their scores at the same time and so need to make them wait in a queue. This is easy to do in other languages like Coldfusion but I can't find how in PHP.


Boo!
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 27th Sep 2004 11:03 Edited at: 27th Sep 2004 11:04
There's probably an easier way to do this in PHP but I remember when I had a similar problem with logging text in VB from mulitple clients, my work-around was to set a character in a file to something like '0' which represented it wasn't being written to. Then when someone tried to save thier text, they'd set it to 1, and once they'd finished, set it back to 0. That way, you'd continously loop until the 1 became a 0 before saving. It's a nasty work-around but I never encountered any problems with it when we had several clients rapidly saving at once.

I wouldn't do that until I was sure PHP didn't have a better way though.


"Computers are useless they can only give you answers."
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 27th Sep 2004 16:26
Exeat: But then what if two clients tried to write the '1' to the file at the same? Then we have the same problem.

Spooky: I'll try and find a solution for you. My WordTrix 2.0 online high scores is run with a MySQL database so I'm not sure how to solve concurrency issues with text files.

Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 27th Sep 2004 16:30
Quote: "Exeat: But then what if two clients tried to write the '1' to the file at the same? Then we have the same problem."


We thought that might happen as well but the time taken to write a couple bytes is unbelievably fast, so fast that it never occured once during our brute force tests. Theoretically it could happen, but it's incredibly unlikely (probably even more unlikely then winning the lottery ).


"Computers are useless they can only give you answers."
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 27th Sep 2004 16:43
Ahhh, of course Maybe you'd have to have 27 million or so people submitting their high score at the EXACT same time to crash out. :-P

David T
Retired Moderator
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 27th Sep 2004 16:50
Use arrays, and serialize / unserialize:



There you go

(haven't tested the multisort, should work)

Get 15 new commands, all the date / time commands left out of DBPro for free!
DOWNLOAD PLUGINS HERE: http://www.davidtattersall.me.uk/ and select "DarkBasic"
Damokles
21
Years of Service
User Offline
Joined: 28th May 2003
Location: Belgium
Posted: 27th Sep 2004 16:57
why not this trick :

- if the highscore file exists, then copy the file on your pc
if it does not exist, run a loop until it exists
- delete the file
- add your score to the file
- upload the file

- Mind the gap -
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 27th Sep 2004 17:03
David T, I think the problem is that 2 seperate browsers could be writing the data at the same time.


"Computers are useless they can only give you answers."
David T
Retired Moderator
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 27th Sep 2004 17:17
Quote: "David T, I think the problem is that 2 seperate browsers could be writing the data at the same time. "


I wasn't trying to solve whatever problem you were doing before - I was simply giving my solution to the overall problem (and a significantly easy solution in my opinion )

I honestly don't think what you're saying will be a problem as execution times are so short (< 0.1s for a short script). Also - does anybody know if php can parse more than one page at the same timr? Just a thought.

Get 15 new commands, all the date / time commands left out of DBPro for free!
DOWNLOAD PLUGINS HERE: http://www.davidtattersall.me.uk/ and select "DarkBasic"
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 27th Sep 2004 17:27
That is the overall problem though. o_O


"Computers are useless they can only give you answers."
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 27th Sep 2004 19:55
Thanks for the help guys.

All I have done so far is the local highscore table and plan on having a 'synchronise' button. This will upload any of your scores to website via an encrypted query string on the end of URL, i.e.

http://www.abc.com/abc.php?q=XXXXXYYYYYZZZZZZ

This will then merge your scores with central database (file on disk), and then output new top 50 to the web browser which using urlmon dll will download to a temp file for dbpro to parse and update local high scores. This is similar to how Banshee Studios Asteroids game does it (except that game uses FTP, which is a bit silly).

It may not be a problem as I invisage the operation to be fairly instant like David T says, but there IS a possibility that two people may try and upload their scores at the same time which 'may' cause a problem.

Best thing I can do for now is write a lock file as operation is started encoded with current date and time, do all the synchronisation and then delete the file.

If someone else tries to upload scores and lock file exists AND date and time it was written is less than, say, 1 minute ago, then write out a 'busy - try again later' message. The idea in using date and time is to stop problem where PHP proggy crashes or never completes and lock file stays, stopping any future uploads.

I hope to get all this working today as game deadline is Thursday.


Boo!
Richard Davey
Retired Moderator
22
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 28th Sep 2004 02:20
Spooky - if you're just passing the highscore on the query string, and only have a local text file to write to, just flock() it.

If two people submit scores at virtually the same time then the first person will lock the highscore table, write to it and then unlock it. The second person will simply sit there waiting while PHP waits for the lock to free up. It will wait for as long as your PHP script time-out value is set (30 seconds usually). Even if the first script somehow dies horribly during the process, PHP will still unlock the file for you when it terminates.

Because flock() is a PHP function you won't have to mess around with a message to people "waiting", it'll happen too quickly. What's more, flock can perform non-blocking locks as well, which means you can write to it while allowing other processes to read.

Even Windows servers support this (which is very nice of them considering how pants they are).

Cheers,

Rich

"I am not young enough to know everything."
- Oscar Wilde
David T
Retired Moderator
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 28th Sep 2004 02:33
Quote: "That is the overall problem though. o_O"


Bah sorry - I thought he meant single thread as in using text file not database :/

Get 15 new commands, all the date / time commands left out of DBPro for free!
DOWNLOAD PLUGINS HERE: http://www.davidtattersall.me.uk/ and select "DarkBasic"
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 28th Sep 2004 09:40
Quote: "just flock() it"


Ahh cool, I knew there was a better way, thanks!

*Writes that function down for later*


"Computers are useless they can only give you answers."
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 28th Sep 2004 10:35
It's just about working without 'flocking' it and so I may add that in today or tomorrow if I get time to test it fully. It runs so damn quickly anyway that I'm not too bothered at the moment.

Hopefully I'll finish game late Tuesday for a few people to test before deadline on Thursday.


Boo!

Login to post a reply

Server time is: 2024-11-26 04:44:06
Your offset time is: 2024-11-26 04:44:06