Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
 Post subject: continuous PHP script
PostPosted: Tue Jun 26, 2012 4:57 am 
Offline
Newbie

Joined: Tue Jun 26, 2012 4:42 am
Posts: 2
I have a PHP script running in a non-stop loop, much of the time it does nothing but every 2-10 minutes it does some actions. So I am running it as a daemon I guess. CPU usage on my linode 512 is a continuous 95%, I asked support if this is acceptable and they said yes since it is percentage of all 4 cores and the maximum is 400% so in effect it is really less than 25%, however I am wondering if there is more effective way to achieve my needs with lower CPU usage. I would have thought the average would be much lower with spikes but the level is flat and constant, it would seem just running a PHP script uses a lot of CPU even if that script is doing nothing other than a code loop.

The reason am doing it this way is I need it to react at certain times of day within accuracy of a few seconds, something I can't achieve with cron jobs, and also it is not the same schedule every day, each day there is a different set of times I need the tasks to run.

Regards,
Geoff


Top
   
PostPosted: Tue Jun 26, 2012 6:05 am 
Offline
Senior Member
User avatar

Joined: Thu Nov 24, 2011 12:46 pm
Posts: 139
Location: Mesa AZ
PHP is not really designed for that purpose. A lot of overhead is running while it is.

If it is email or web generated, it should be easy to have it trigger an event which causes it to be processed. The trigger could be a shell, Perl or other script which can call PHP if necessary to perform the action.

Not really enough info provided.

_________________
Kevin a.k.a. Dweeber


Top
   
PostPosted: Tue Jun 26, 2012 6:07 am 
Offline
Senior Member
User avatar

Joined: Sat Aug 30, 2008 1:55 pm
Posts: 1739
Location: Rochester, New York
What is it doing during the loop if it's not time for it to run? If it's not calling sleep() somewhere in there, yes, it will burn up a lot of CPU. Try sticking sleep(1) in there, or whatever PHP's notation for it is.

_________________
Code:
/* TODO: need to add signature to posts */


Top
   
PostPosted: Tue Jun 26, 2012 6:24 am 
Offline
Newbie

Joined: Tue Jun 26, 2012 4:42 am
Posts: 2
Many thanks!, adding a sleep(1) into every loop cycle has made a MASSIVE difference, now the CPU usage is almost nothing while it's looping doing nothing, spiking at 15-20% only when the other time triggered activities occur.


Top
   
PostPosted: Tue Jun 26, 2012 6:31 am 
Offline
Senior Member
User avatar

Joined: Sat Aug 30, 2008 1:55 pm
Posts: 1739
Location: Rochester, New York
Indeed, CPUs are really good at doing nothing. :-) Glad to know that helped!

_________________
Code:
/* TODO: need to add signature to posts */


Top
   
PostPosted: Tue Jun 26, 2012 10:58 am 
Offline
Senior Member
User avatar

Joined: Tue May 26, 2009 3:29 pm
Posts: 1691
Location: Montreal, QC
How is it reacting? If it's listening for incoming packets or connections, you should be using socket_select(), which will block waiting on activity on any of the sockets, and immediately wake up your app when there is activity. It also has a timeout, so you could set it to time out once a second to do other stuff periodically, for example.

Does anybody remember that name of the PHP framework for writing daemons? The one whose philosophy was something like "PHP usually isn't the best thing to do this in, but if you have to, this will make it as easy as possible"?


Top
   
PostPosted: Tue Jun 26, 2012 11:03 am 
Offline
Senior Member
User avatar

Joined: Wed Apr 20, 2011 1:09 pm
Posts: 63
Guspaz wrote:
How is it reacting? If it's listening for incoming packets or connections, you should be using socket_select(), which will block waiting on activity on any of the sockets, and immediately wake up your app when there is activity. It also has a timeout, so you could set it to time out once a second to do other stuff periodically, for example.

Does anybody remember that name of the PHP framework for writing daemons? The one whose philosophy was something like "PHP usually isn't the best thing to do this in, but if you have to, this will make it as easy as possible"?


https://github.com/shaneharter/PHP-Daemon

Warning, have lube on standby. Doing this kind of thing in PHP is not gentle.

_________________
うるさいうるさいうるさい!


Top
   
PostPosted: Tue Jun 26, 2012 11:11 am 
Offline
Senior Member
User avatar

Joined: Tue May 26, 2009 3:29 pm
Posts: 1691
Location: Montreal, QC
I wrote a long-lived PHP daemon back in the day (years ago, an IRC bot that managed some channels, supported custom plugins, etc) and it seemed to work pretty well. What makes PHP or Java well or poorly suited for writing a daemon? Is it just a matter of an inefficient garbage collector or something?

PS: Writing the IRC bot was a fun exercise. Funny how a lot of the programming-for-fun that I do falls into the "useless or unnecessary but fun" category. Loading plugins dynamically by including them when executed, for example, is basically a memory leak. Running the plugins in a separate instance of the PHP interpreter with a standardized manner of passing information back and forth, on the other hand, worked better.


Top
   
PostPosted: Tue Jun 26, 2012 11:16 am 
Offline
Senior Member
User avatar

Joined: Wed Apr 20, 2011 1:09 pm
Posts: 63
Guspaz wrote:
I wrote a long-lived PHP daemon back in the day (years ago, an IRC bot that managed some channels, supported custom plugins, etc) and it seemed to work pretty well. What makes PHP or Java well or poorly suited for writing a daemon? Is it just a matter of an inefficient garbage collector or something?

PS: Writing the IRC bot was a fun exercise. Funny how a lot of the programming-for-fun that I do falls into the "useless or unnecessary but fun" category. Loading plugins dynamically by including them when executed, for example, is basically a memory leak. Running the plugins in a separate instance of the PHP interpreter with a standardized manner of passing information back and forth, on the other hand, worked better.


Mmm, GC is actually nicely done in 5.3+, that's not the real issue - threading/forking is when you start getting into very very painful things. Single-threaded daemons that don't mind blocking is fine, but non-blocking execution is absolutely frightening.

(I wrote my own IRC bot in php as well, turned out quite well with uptimes of up to two months without leakage or breakdowns of any sort, and was a great learning experience - am currently rewriting her in node.js to learn node itself, and take advantage of node's async behavior)

_________________
うるさいうるさいうるさい!


Top
   
PostPosted: Tue Jun 26, 2012 11:57 am 
Offline
Senior Member
User avatar

Joined: Tue May 26, 2009 3:29 pm
Posts: 1691
Location: Montreal, QC
Threading/forking in PHP is just barebones POSIX stuff, though... It should be the same experience as doing it in C, no?


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group