tags:

views:

302

answers:

8

There is some way to execute a php script every 40 miliseconds? I don't know if cronjob is the right way, because 25 times per second require a lot of CPU.

Well, If php isn't the correct language, what language I should use?

I am making a online game, but I need something to process what is happening in the game, to move the characters, to calculate projectiles paths, etc.

+5  A: 

PHP is a slow, interpreted language. For it to open a file takes almost that amount of time. Rxecuting a PHP script every 40 milliseconds would lead to a huge queue, and a crash very quickly. This definitely sounds like a task you don't want to use PHP for, but a daemon or other fast, compiled binary. What are you looking to do?

Pekka
+1  A: 

As far as I know a cronjob can only be executed every minute. That's the smallest amount of time possible. I'm left wondering why you need such a small amount of time of execution?

TheGrandWazoo
+6  A: 

Every 40 milliseconds would be impressive. It's not really suited for cron, which runs on 1-minute boundaries.

Perhaps if you explained why you need that level of performance, we could make some better suggestions.

Another thing you have to understand is that it takes time to create processes under UNIX - this may be better suited to a long running task started once and just doing the desired activity every 40ms.

Update: For an online game with that sort of performance, I think you seriously need to consider having a fat client running on the desktop.

By that I mean a language compiled to machine language (not interpreted) and where the bulk of the code runs on the client, using the network only for transmitting information that needs to be shared.

I don't doubt that the interpreted languages are suitable for less performance intensive games but I don't think, from personal experience, you'll be able to get away with them for this purpose.

paxdiablo
A: 

If you really want it to be PHP, I guess you should keep the process running through a shell, as some kind of deamon, instead of opening/closing it all the time.

I do not know how to do it but I guess you can at least get some inspiration from this post:

http://kevin.vanzonneveld.net/techblog/article/create%5Fdaemons%5Fin%5Fphp/

phidah
+13  A: 

If you try to invoke a PHP script every 40 milliseconds, that will involve:

  • Create a process
  • Load PHP
  • Load and compile the script
  • Run the compiled script
  • Remove the process and all of the memory

You're much better off putting your work into the body of a loop, and then using time_sleep_until at the end of the loop to finish out the rest of your 40 milliseconds. Then your run your PHP program once.

Keep in mind, this needs to be a standalone PHP program; running it out of a web page will cause the web server to timeout on that page, and then end your script prematurely.

Craig Trader
40 milliseconds is probably such a small amount of time you wouldn't even need any sleeping, the update itself is likely to take this long.
Swizec Teller
A: 

As everyone else is saying, starting a new process every 40ms doesn't sound like a good idea. It would be interesting to know what you're trying to do. What do you want to do if one execution for some reason takes more than 40ms? If you're now careful you might get lots of processes running simultaneous stepping on each other toes.

What language will depend a lot on what you're trying to do, but you should chose a language with thread support so you don't have to fork a new process all the time. Java, Python might be suited.

Kimble
I've added some informations about what I want to do.I don't know anything about python, it would take some weeks to I learn a new programming language.
M28
A: 

I'm not so sure every 40 MS is realistic if the back end job has to deal with things like database queries. You'd probably do better working out a way to be adaptive to system conditions and trying hard to run N times per second, rather than every 40 MS like clockwork. Again, this depends on the complexity of what you need to accomplish behind the curtain.

PHP is probably not the best language to write this with. This is for several reasons:

  • Depending on the version of PHP, garbage collection may be broken. If you daemonize, you run a risk of leaking memory N times a second.

  • Other reasons detailed in this answer.

Try using C or Python and keep track of how long each iteration takes. This lets you make a 'best effort' to run N times a second, or every 40 MS, whichever is greater. This avoids your process perpetually running since every time it finishes, its already late to get started again.

Again, I'm not sure how long these tasks should take on a 'worst case' scenario system load .. so my answer may or may not apply in full. Regardless, I advise you to not write a stand alone daemon in PHP.

Tim Post
I don't have any knowledge about python and I have never created something so big using C
M28
A: 

PHP is the wrong language for this job. If you want to do something updating that fast in a browser, you need to use Javascript. PHP is only for the backend, which means everything PHP does has to be send from your server to the browser and then rendered.

Brendan Long
But this processing cannot be handle by the user because it process essential things in the game.
M28
I wrote a game like this a while ago (not very big), but how I did it was to have Javascript do everything on the user side, and also send the commands to the server. Every once in a while it would check with the server to make sure the game state is the same in both, and then refresh the page if they're different. I hate to recommend it, but I hear Flash is good for this sort of thing too.
Brendan Long