开发者

PHP Memory Usage during sleep and loops

开发者 https://www.devze.com 2023-02-17 20:38 出处:网络
I have a few questions about PHP memory usage. I\'m going to run some tests on my own, but getting various advice is quite helpful.

I have a few questions about PHP memory usage. I'm going to run some tests on my own, but getting various advice is quite helpful.

I recently learned about the PHP function ignore_user_abort(), which allows a script to continue running even if a user closes the page. I was thinking about using this for my E-mail Newsletter tool instead of Cron jobs, as configuring Cron jobs has various pitfuls. The alternative approach of making a user stay on the page, using AJAX requests, and running part of the script after the page content has been delivered all have issues as well.

My solution would be to run call ignore_user_abort(true) at the beginning of the script, and at the end after the content has been generated, call flush() for good measure, and then run the newsletter script. Alternatively, do this with an AJAX.

First of all, does anyone see issues with that approach?

Second of all, if I used the script with no time limit set, and a while loop going through each email, what would the memory usage be like if I did it in one go? Since I'd be overwriting variables, not using new ones, I'd think it would be low.

Third, because if I am sending a large volume of emails, say 1000 per run, I don开发者_Go百科't want to overload my mail server. With my cron job, I run the script every 5 minutes, sending a batch of 50 emails out. If I was doing this in a single pass, could I send out 50 emails, call sleep for say 5 minutes, and then continue for another 50 emails? If so, what is the script memory usage like during the sleep period? Would this be an efficient method?

What I'm really trying to do here is come up with a way to create a newsletter tool that doesn't require the complex (for non-technical folks) task of setting up a Cron job (Which isn't even an option on shared hosts), and doesn't require the user to keep their browser open on a single page.

Any ideas suggestions or feedback is welcome. Thanks!


At a former job we wrote a daemon for a critical function in PHP, not exactly what you describe but similar enough -- certainly with loops and sleeps. We were very doubtful about its long-term stability -- specially in memory management--, so we subjected it to pretty tough stress testing. Results were excellent, and the code was put to production and running flawlessly for months if not years.

Caveats:

  • IIRC, PHP has a counter-based garbage collector. This means that, unlike in Java, two objects referencing each other will stay in memory even if not accessible by your program. You need to be careful about this when you 'abandon' your objects.
  • Web servers often have mechanisms to kill long-running scripts. This may defeat your purpose here -- specially if the server's configuration can't be tuned.
0

精彩评论

暂无评论...
验证码 换一张
取 消