Page 1 of 1

PHP Run proces

Posted: Tue Mar 23, 2010 7:11 am
by kirank
Hi,

I want to run a PHP Program in background for 24 hours
I think i can start the program using a Crone job and it will work for 24 hours by coding it
When i tried it with an infinite loop, my program takes 99% of CPU time


How can i implement this in better way avoding infinite while loop.
I waanna run some code snippet in the program continuously, ex: checking a Database Table for
new entries.

Which is the better way to achieve this eliminating server over heads.

Thanks in Advance

Re: PHP Run proces

Posted: Tue Mar 23, 2010 7:14 am
by Eran
An infinite loop is the only way to keep a script running indefinitely. Make the script sleep at the end of each iteration to avoid it looping too fast and consuming all the CPU cycles.
http://www.php.net/manual/en/function.usleep.php

Re: PHP Run proces

Posted: Thu Mar 25, 2010 3:02 am
by kirank
Hi,

I appreciate the solution. But see, i want to run more that 6 programs in infinite loop. Sometimes more than 10 programs needs to be exected in this way.
One program will start as a main program, and it will create more than 10 child programs (these are having infinite loops) . Crone can be used for thsi or not ?? please give a good solution .

Thanks

Re: PHP Run proces

Posted: Thu Mar 25, 2010 4:03 am
by Zlobcho
I would go for gearman. You can have a trigger to run jobs (might be background jobs) on new entry. Go and read some more on gearman and see if it will work out for you. :)

Re: PHP Run proces

Posted: Sun Mar 28, 2010 6:13 am
by kirank
Yeah i agree.
But i tried google and find some useful resources regarding process forging. Then i developed one simple PHP Library for running background multiple processes. Hope this will be useful for creating threads / background process in easy way .

http://whyscript.com/packages/OSX/

See this

Thanks