This is a discussion on Re: infinite looping script within the PHP Language forums, part of the PHP Programming Forums category; In alt.comp.lang.php Gu <pistacchio@gmail.com> wrote: >On Mon, 25 Apr 2005 16:25:...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
In alt.comp.lang.php
Gu <pistacchio@gmail.com> wrote: >On Mon, 25 Apr 2005 16:25:45 GMT, bruce@nospam.com (Bruce) wrote: > >>I'd like to create a script that does some online processing for me, 24 >>hours a day, adding results to a database. It would essentially be a >>worker thread. I'm not sure how to accomplish this with php. >> >>something like this >> >>for (;;) >>{ >> // Get current temperature in Barrow, Alaska >> // Get Current time/date >> // Add these values to MySQL database >> // sleep(60) >>} >> >>So if I load the page, this script will run but I need it to run forever. > >you can do it in a lot of ways, the first that comes to my mind is: > >$forever=TRUE; >WHILE ($forever) { > your loop.. >} > >keep in mind that if your script is hosted somewhere like a public >hosting service (and / or a shared machine) they would possibly not >allow you to have an infinite loop and would break it automatically >after a certain amount of iterations. such processes are cpu-killing. Ok, I understand they can be cpu killing, but that is the point of the sleep(60), right? Its only going to run once a minute. I'm not going to put up something that runs wide open all the time, never yielding. >a similar, and a bit more fair, result could be accoplished putting >your script inside a auto-reloading page: > ><meta http-equiv="refresh" content="10"> Won't this only run when a page is displayed in a browser? I need a background task constantly updating the database. It looks like the pctrl functions would do it but my server does not have them installed. I'm looking at popen() and procopen() but I don't think they'll do it. I wish I could just spawn a worker thread. |
|
|||
|
Bruce wrote:
> > I'd like to create a script that does some online processing for me, 24 > hours a day, adding results to a database. It would essentially be a > worker thread. I'm not sure how to accomplish this with php. You shouldn't do it in an interpreted language. Apache allocates several hundred kilobytes to each instance. Command-line interpreter would eat up a couple of megabytes. A properly written daemon/service will most likely do the job for under 100k. > for (;;) > { > // Get current temperature in Barrow, Alaska > // Get Current time/date > // Add these values to MySQL database > // sleep(60) > } A more elegant solution would be to use an OS-level scheduling facility (cron on Unix or at/Scheduler on Windows). > I understand they can be cpu killing, but that is the point of the > sleep(60), right? Its only going to run once a minute. What about memory and connections? They will still remain allocated to the script and will not be available to other processes... Let's say your script runs for two seconds every minute. Calling it every minute would use the memory and MySQL connection for two seconds. Keeping the task running will result in those same resources being permanently allocated to the task. > I need a background task constantly updating the database. No you don't. You need a task that will run once a minute for a couple of seconds. You said so yourself. Cheers, NC |
|
|||
|
In comp.lang.php
"NC" <nc@iname.com> wrote: >You shouldn't do it in an interpreted language. Apache allocates >several hundred kilobytes to each instance. Command-line interpreter >would eat up a couple of megabytes. A properly written daemon/service >will most likely do the job for under 100k. Written in what language? This is a remote server and I may not have the necessary access to do this. >A more elegant solution would be to use an OS-level scheduling facility > >(cron on Unix or at/Scheduler on Windows). Yep, I'm looking at cron right now. It think that is the solution. >> I need a background task constantly updating the database. > >No you don't. You need a task that will run once a minute for a >couple of seconds. You said so yourself. I used the term "constantly" a bit loosely. In any event, you are correct and I think cron calling a php module will work just fine. thanks |