Re: infinite looping script

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:...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-25-2005
Bruce
 
Posts: n/a
Default Re: infinite looping script

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.
Reply With Quote
  #2 (permalink)  
Old 04-25-2005
NC
 
Posts: n/a
Default Re: infinite looping script

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

Reply With Quote
  #3 (permalink)  
Old 04-25-2005
Bruce
 
Posts: n/a
Default Re: infinite looping script

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
Reply With Quote
  #4 (permalink)  
Old 04-27-2005
hackajar@gmail.com
 
Posts: n/a
Default Re: infinite looping script

In case you want a PHP loop for something else

<?php

While(0) {
$temp =
file_get_contents("http://www.example.org/?zip=95012&unit=temp");
sleep(60);
}

?>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:07 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0