Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm actually work on <a href="https://stackoverflow.com/questions/3115191/scalable-delayed-php-processing">something</a> very close to what you described, but in my case the daemon doesn't poll for event it get's them asynchronously via XMPP (but that's besides the point).</p> <p><strong>Cut out the Middle Man</strong></p> <p>I think instead of storing the events in the database and polling them w/ MySQL you could probably use <a href="http://gearman.org/" rel="nofollow noreferrer">Gearman</a> to send them from the client asynchronously (<a href="http://gearman.org/index.php?id=gearman_php_extension" rel="nofollow noreferrer">example</a>).</p> <p><strong>Garbage Collection</strong></p> <p>PHP isn't really designed to run as a daemon, and it wasn't until PHP 5.3 when it got <a href="http://www.ibm.com/developerworks/opensource/library/os-php-5.3new1/index.html#N101D4" rel="nofollow noreferrer">circular reference garbage collection</a> that it became a viable option. It's <strong>very</strong> important that you use PHP 5.3 if you want any chance at long term running without memory leaks.</p> <p>Another thing to keep in mind about GC is that memory is only free if it's not longer referenced (anywhere). So if you assign a variable to the global scope, it'll be there until the daemon exits. It's important that any code you create or use doesn't built up variables in places (ie, static log, not removing old data, etc). </p> <p><strong>Stat Cache</strong></p> <p>Another thing is that it's important to run <code>clearstatcache</code> every so often. Since your PHP process isn't restarted it's important to do this call manually to prevent getting old stat data (which may or may not effect you). According to the <a href="http://php.net/manual/en/function.clearstatcache.php" rel="nofollow noreferrer">documentation</a> these functions are cached.</p> <blockquote> <p>Affected functions include stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), and fileperms().</p> </blockquote> <p><strong>Resource management</strong></p> <p>If your going to be using thing like MySQL during the lifetime of your process, I'd suggest making one connection at startup and keeping it alive. Even though it might use more ram on the MySQL side, you'll cut out some latency and CPU overhead by not having to connect every 1 second.</p> <p><strong>No URL request</strong></p> <p>This may seem obvious, but with CLI PHP there is no URL request info. Some libraries aren't written with this in mind, and this can cause some problems.</p> <p><strong>LooPHP</strong></p> <p>I'm going to pop a shameless plug in here for a framework I wrote to help with the management of PHP daemons. <a href="https://github.com/Wordi/LooPHP" rel="nofollow noreferrer">LooPHP</a> is a run loop framework that lets you schedule event to happen or create listens for abstract sources (socket, stream, etc). In my case, I have the daemon doing more than 1 thing, so it's very helpful to have system keep track of all the timers for me so that I can effectively poll <code>stream_select</code> for the XMPP connection.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload