Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
$this->stop_server = true;
exit(1);
while (!$this->stop_server)
while(TRUE)
$Flag = 'daemon.flag';
$Self = daemon.php;
if (!file_exists($Flag))
if (strtoupper(substr(PHP_OS,0,3)) === 'WIN') {
touch($Flag);
$fp = popen('start /B php -f '.$Self,'r');
if ($fp!==false) pclose($fp);
unlink($Flag);
}
} else {
// Child process code
}
ps -AF
kill -9 PID
php /path/to/script
<?php
// Somewhere before the endless loop
$last_gc_cycle = time() - (24 * 3600);
// Some more code
while (true) {
// The main code here
if (function_exists('gc_collect_cycles')) {
$time = time();
if ($time - $last_gc_cycle > 300) {
$last_gc_cycle = $time;
gc_collect_cycles();
}
}
}
// Без этой директивы PHP не будет перехватывать сигналы
declare(ticks=1);
Для background задач вроде обработки очередей — не вижу смысла в демонах.
Через msg_*.
Если скрестить со «скелетом» из топика, то не сложно, наверное, сделать и параллельную обработку нескольких задач, если это имеет смысл.
$queue = msg_get_queue(12345);
$message = NULL;
$msgtype = NULL;
while (msg_recieve($queue, 0, $msgtype, 256, $message)) {
dispatchMessage($msgtype, $message); // обрабатываем сообщение
}
$message = 'I have a task for daemon.';
$queue = msg_get_queue(12345);
msg_send($queue, $msgtype, $message);
Демоны на PHP