PHP Daemon in PHP 5.3
While the idea of a PHP daemon has been covered to death on here, I haven't found anything specifically related to how to do this in PHP 5.3. As I've been told, 5.3 introduced new garbage collection/memory management to allow PHP to more cleanly run as a daemon.
I know PHP's no one's first choice for this kind of thing, but in my circumstances it might have to do.
I know in PHP 4, you would have to use somet开发者_StackOverflow中文版hing like the System_Daemon class, but I was wondering if that was still needed with the new version of PHP, and wether I'd need to do anything special to use the new features.
PHP uses reference counting for managing allocated memory. When a cycle exists between objects, their reference count is never decremented and the objects are never freed (until the end of the script).
The only goal of the garbage collector added in PHP5.3 is to kill those cycles. This effectively helps in reducing the memory usage of long running scripts, like daemons.
Other than that, PHP5.3 adds nothing new for long running scripts / daemons.
There has been some efforts in making app servers in PHP lately, you may want to look at them:
https://github.com/indeyets/appserver-in-php
The garbage collector is an internal thing. It does not change how you write a daemon. And there was just a more inefficient form of garbage collection (resource freeing) before PHP 5.3, that's all. http://php.net/manual/en/features.gc.performance-considerations.php
You are supposed to still fork daemon processes, as there is no threading support to use instead. And this implicitly takes care of freeing memory, so it doesn't practically matter.
精彩评论