简体   繁体   English

不要等待进程退出

[英]Don't wait for the process to exit

I have a PHP script that is called from a cron job every minute.我有一个每分钟从 cron 作业调用的 PHP 脚本。 This script takes some info from the database and then calls another PHP script using the System function (passing it some parameters).该脚本从数据库中获取一些信息,然后使用 System 函数(向其传递一些参数)调用另一个 PHP 脚本。

That means that I can start up to 10 scripts from this "main" one.这意味着我可以从这个“主要”脚本启动多达 10 个脚本。 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?如何才能做到这一点?

You may be able to use proc_open() , stream_select() and stream_set_blocking() in concert to achieve this kind of thing.您可以proc_open()使用proc_open()stream_select()stream_set_blocking()来实现这种事情。

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 cronjob 调用 cronjob_wrapper.php
  2. cronjob_wrapper.php creates a new Manager class and then calls start on it. cronjob_wrapper.php 创建一个新的 Manager 类,然后调用start
  3. Manager class start method check to see how many instances are running (looking for pid files in a particular location).管理器类启动方法检查有多少实例正在运行(在特定位置查找 pid 文件)。 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如果它小于给定的最大实例数,它将自己的进程 ID 写出到 pid 文件,然后继续
  4. Manage class creates an instance of an appropriate Encoder class and calls exec on it. Manage 类创建一个适当的 Encoder 类的实例并对其调用exec
  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!) exec方法使用 proc_open、stream_select 和 stream_set_blocking 以非阻塞方式运行系统命令(在这种情况下运行 ffmpeg - 可能需要很长时间!)
  6. When it has finally run it cleans up its PID file and bails out.当它最终运行时,它会清理它的 PID 文件并退出。

Now the reason I'm being vague and handwavy is that our multiple instances here are being handled by the cronjob not by PHP.现在我含糊不清的原因是我们这里的多个实例是由 cronjob 处理的,而不是由 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.我试图做你所说的那种事情,并且与pcntl_fork()和朋友们一起工作得很好,但最后我遇到了几个问题(如果我记得至少有一个是PHP),并认为这种方法是实现相同目标的更加坚如磐石的方式。 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... :)尽管不知何故,我认为 PHP 永远不会成为首选的套接字编程语言...... :)

If your OS supports it, you can use the pcntl_fork() function to spin off child processes that the parent doesn't wait for.如果您的操作系统支持它,您可以使用pcntl_fork()函数来分离父进程不等待的子进程。 Be careful though, it is easy to accidentally create too many child processes, especially if they take longer than expected to run!不过要小心,很容易意外创建过多的子进程,尤其是当它们运行时间比预期长的时候!

I'm not sure that PHP supports threading.我不确定 PHP 是否支持线程。 Check here .检查这里

我认为答案与已经为异步 PHP 调用提供的答案非常相似。

http://php.net/pcntl_fork http://php.net/pcntl_fork

It's *NIX only but you can fork your script using the PCNTL extension.它只是 *NIX,但您可以使用 PCNTL 扩展来派生您的脚本。

使用 php 版本的 fork 或线程。

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. PHP 并不完全知道用作守护程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM