不要等待进程退出 [英] Don't wait for the process to exit

查看:76
本文介绍了不要等待进程退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,每分钟都会从cron作业中调用.该脚本从数据库中获取一些信息,然后使用System函数(向其传递一些参数)调用另一个PHP脚本.

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).

这意味着我可以从这个主要"脚本中启动多达10个脚本.我想做的是,我将调用该脚本并继续执行主脚本,也就是说,不要等待系统调用完成,然后再调用下一个脚本.

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.

这怎么办?

推荐答案

您也许可以使用 proc_open() stream_select() stream_set_blocking() 共同实现这种事情.

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调用cronjob_wrapper.php
  2. cronjob_wrapper.php创建一个新的Manager类,然后在其上调用 start .
  3. Manager类的启动方法检查以查看正在运行的实例数(在特定位置查找pid文件).如果它少于给定的最大实例数,则会将其自己的进程ID写入pid文件,然后继续进行
  4. 管理类创建一个适当的Encoder类的实例,并在其上调用 exec .
  5. exec 方法使用proc_open,stream_select和stream_set_blocking以非阻塞方式运行系统命令(在这种情况下,运行ffmpeg-可能需要一段时间!)
  6. 当它最终运行时,它将清理其PID文件并解救.
  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.

现在,我变得晦涩难懂的原因是,这里的多个实例是由cronjob处理的,而不是由PHP处理的.我正在尝试做您正在谈论的那种事情,并且使 pcntl_fork() 和朋友,但是最后我遇到了两个问题(如果我记得至少一个是PHP中的错误),并决定这种方法要困难得多. -实现同一件事的坚实方法. YMMV.

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.

尽管值得一看,但您可以通过它们实现很多功能.虽然我不知道PHP不会成为首选的套接字编程语言...:)

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... :)

这篇关于不要等待进程退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆