Is it wise to use PHP for a daemon?

前端 未结 17 1410
庸人自扰
庸人自扰 2020-11-28 09:18

I wish to create a background process and I have been told these are usually written in C or something of that sort. I have recently found out PHP can be used to create a da

相关标签:
17条回答
  • 2020-11-28 09:58

    One problem with properly daemonizing a PHP script is that PHP doesn't have interfaces to the dup() or dup2() syscalls, which are needed for detaching the file descriptors.

    0 讨论(0)
  • 2020-11-28 10:00

    If you combine the answers from Kent Fredric, tokenmacguy and Domster you get something useful.

    php is probably not good for long execution times, so let's keep every execution cycle short and make sure the OS takes care of the cleanup of any memoryleaks. As a tool to start your php script cron can be a good tool. And if you do it like that, there is not much difference between languages.

    However, the question still stands. Is php even capable to run as a normal daemon for long times (some years)? Or will assorted memoryleaks eat up all your ram and kill the system?

    /Johan

    0 讨论(0)
  • 2020-11-28 10:01

    I would be inclined to perform this task with a cron job, rather than polling the database in a daemon.

    It's likely that your FFmpeg command will take a while to do it's thing, right? In that case, is it really necessary to be constantly polling the database? Wouldn't a cronjob running each minute (or every five, ten or twenty minutes for that matter) be a simpler way to achieve the same thing?

    0 讨论(0)
  • 2020-11-28 10:01

    Cron job? Yes.

    Daemon which runs forever? No.

    PHP does not have a garbage collector (or at least, last time I checked it did not). Therefore, if you create a circular reference, it NEVER gets cleaned up - at least not until the main script execution finishes. In daemon process this is approximately never.

    If they've added a GC in new versions, then yes you can.

    0 讨论(0)
  • 2020-11-28 10:02

    Running as a cron job with sensibly determined periodicity, a PHP script can do the job, and production stability is certainly achievable. You might want to limit the number of simultaneous FFMpeg instances, and be sure to have complete application logging and exception handling. I have implemented continuously running polling processes in Java, as well as the every-ten-minute cron'd PHP script, and both do the job nicely.

    0 讨论(0)
  • 2020-11-28 10:05

    If you do so, pay attention to memory leaks. PHP 5.2 has some problems with its garbage collector, according to this (fixed in 5.3). Perhaps its better to use cron, so the script starts clean every run.

    0 讨论(0)
提交回复
热议问题