why do I need to make my Script sleep?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kelmadics
Forum Newbie
Posts: 2
Joined: Tue Sep 11, 2007 11:32 am

why do I need to make my Script sleep?

Post by kelmadics »

Just curious why is it sometimes necessary to make a code sleep() specially if
the code is long running script? I know its a dumb q but your knowledge would help
Thanks
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

Last time i used it to check if image caching was working properly in IE6.
Getting image from server (localhost) was very fast, so i needed something to slow it down to see if they were from cache or from server, so i just returned image to browser after sleep().
kelmadics
Forum Newbie
Posts: 2
Joined: Tue Sep 11, 2007 11:32 am

Post by kelmadics »

what i was actually wondering is how to make memory more efficient while running a script. Lets say i am sending an eblast with 100000 emails
if i query those emails using mysql_fetch_array, those emails will be stored in memory. what i wanted to do is dump the emails that has already run thrugh the script. So let say ive sent the first 100 emails of the 100000 i want to dump those 100 in the array memory because its useless. Mistakenly I was thinking that the sleep commands "refreshes" the memory of the script. please correct me.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sleep() does little as far as memory is concerned. It is used to prevent your script from inhaling the server's processor(s), nothing more.

If these emails are sent per record you aren't sucking up major amounts of memory as far as PHP is concerned provided you don't have PHP store more than a record. Generally there's little reason to store more than the current record if you're sending customized emails.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

feyd wrote:If these emails are sent per record you aren't sucking up major amounts of memory as far as PHP is concerned provided you don't have PHP store more than a record.
Which means that you should use an unbuffered query, like mysql_unbuffered_query() or PDO

You might want to consider something like majordomo instead of a selfmade php script for your non-spam opt in/out mailing list
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

sleep() is important in infinite loops to delay the script and prevent it from using up all available memory.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post by mrkite »

scottayy wrote:sleep() is important in infinite loops to delay the script and prevent it from using up all available memory.
I thought gc would run regardless. sleep() would prevent cpu hogging.
Post Reply