Don't wait for the process to exit

2020-02-07 06:42发布

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 (passing it some parameters).

That means that I can start up to 10 scripts from this "main" one. And what I would like to do is that I would call the script and continue the execution of the main script, that is, not wait for the System call to complete and then call the next one.

How can this be done?

标签: php
7条回答
女痞
2楼-- · 2020-02-07 07:06

You may be able to use proc_open(), stream_select() and stream_set_blocking() in concert to achieve this kind of thing.

If that sounds vague, I was going to paste a big chunk of code in here that I used in a recent project that did something similar, but then felt it may hinder rather than help! In summary though, the code worked like this:

  1. cronjob calls cronjob_wrapper.php
  2. cronjob_wrapper.php creates a new Manager class and then calls start on it.
  3. Manager class start method check to see how many instances are running (looking for pid files in a particular location). If it's less than a given max number of instances it writes out it's own process id to a pid file and then carries on
  4. Manage class creates an instance of an appropriate Encoder class and calls exec on it.
  5. The exec method uses proc_open, stream_select and stream_set_blocking to run a system command in a non-blocking fashion (running ffmpeg in this case - and could take quite a while!)
  6. When it has finally run it cleans up its PID file and bails out.

Now the reason I'm being vague and handwavy is that our multiple instances here are being handled by the cronjob not by PHP. I was trying to do very much the kind of thing you are talking about, and got something working pretty well with pcntl_fork() and friends, but in the end I encountered a couple of problems (if I recall at least one was a bug in PHP) and decided that this approach was a much more rock-solid way to achieve the same thing. YMMV.

Well worth a look at those functions though, you can achieve a lot with them. Though somehow I don't think PHP will ever become the sockets programming language of choice... :)

查看更多
淡お忘
3楼-- · 2020-02-07 07:07

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

查看更多
Juvenile、少年°
4楼-- · 2020-02-07 07:11

use php's version of fork or threads.

查看更多
Root(大扎)
5楼-- · 2020-02-07 07:12

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.

查看更多
Viruses.
6楼-- · 2020-02-07 07:18

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

查看更多
走好不送
7楼-- · 2020-02-07 07:19

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!

查看更多
登录 后发表回答