Don't wait for the process to exit

后端 未结 7 2147
生来不讨喜
生来不讨喜 2020-12-12 03:17

I have a PHP script that is called from a cron job every minute. This script takes some info from the database and then calls another PHP script using the System function (p

相关标签:
7条回答
  • 2020-12-12 03:33

    If your OS supports it, you can use the pcntl_fork() function to spin off child processes that the parent doesn't wait for. Be careful though, it is easy to accidentally create too many child processes, especially if they take longer than expected to run!

    0 讨论(0)
  • 2020-12-12 03:36

    use php's version of fork or threads.

    0 讨论(0)
  • 2020-12-12 03:36

    You could run them in the background:

    system('php yourscript.php &');
    

    You just have to make sure that you check on the total number of processes running. All in all, not a super elegant solution. Instead cron you could let one script run for forever, I am thinking something like this:

    <?php
    while(true) {
      // do whatever needs to be done.
    }
    ?>
    

    Careful though. PHP is not exactly known to be used as a daemon.

    0 讨论(0)
  • 2020-12-12 03:42

    http://php.net/pcntl_fork

    It's *NIX only but you can fork your script using the PCNTL extension.

    0 讨论(0)
  • 2020-12-12 03:44

    I think the answer would be very similar to those already provided for Asynchronous PHP calls.

    0 讨论(0)
  • 2020-12-12 03:47

    I'm not sure that PHP supports threading. Check here.

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