简体   繁体   English

PHP 5.3中的PHP守护程序

[英]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. 尽管此处已经涵盖了PHP守护程序的概念,但我还没有发现与如何在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. 据我所知,5.3引入了新的垃圾收集/内存管理,以使PHP可以更干净地作为守护程序运行。

I know PHP's no one's first choice for this kind of thing, but in my circumstances it might have to do. 我知道PHP并不是此类事情的第一选择,但是在我的情况下,它可能必须这样做。

I know in PHP 4, you would have to use something 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 4中,您将不得不使用类似System_Daemon类的类,但是我想知道新版本的PHP是否仍然需要这样做,并且我还需要做一些特殊的事情才能使用这些新功能。

PHP uses reference counting for managing allocated memory. PHP使用引用计数来管理分配的内存。 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. PHP5.3中添加的垃圾收集器的唯一目标是消除这些循环。 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. 除此之外,PHP5.3对于长时间运行的脚本/守护程序没有添加任何新内容。

There has been some efforts in making app servers in PHP lately, you may want to look at them: 最近在用PHP制作应用服务器方面已经作了一些努力,您可能要看一下它们:

https://github.com/indeyets/appserver-in-php 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. 在PHP 5.3之前,垃圾回收(资源释放)的效率较低,仅此而已。 http://php.net/manual/en/features.gc.performance-considerations.php 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. 而且这隐式地负责释放内存,因此实​​际上并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM