user216441
user216441

Reputation:

Execute php script every 40 milliseconds?

There is some way to execute a php script every 40 milliseconds? 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.

Upvotes: 3

Views: 2811

Answers (8)

Pekka
Pekka

Reputation: 449435

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?

Upvotes: 5

Brendan Long
Brendan Long

Reputation: 54242

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.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881463

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.

Upvotes: 6

user50049
user50049

Reputation:

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.

Upvotes: 0

Kimble
Kimble

Reputation: 7554

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.

Upvotes: 0

Craig Trader
Craig Trader

Reputation: 15679

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.

Upvotes: 13

fiskah
fiskah

Reputation: 5902

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_daemons_in_php/

Upvotes: 0

TheGrandWazoo
TheGrandWazoo

Reputation: 2897

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?

Upvotes: 1

Related Questions