从另一个内部生成一个 PHP 脚本.无阻塞 [英] Spawning a PHP script from inside another. Non-blockning

查看:33
本文介绍了从另一个内部生成一个 PHP 脚本.无阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PHP 脚本,我需要从另一个 PHP 网页中执行该脚本.但是,要使第二个正常运行,第一个需要完全完成.本质上,我需要第一页为第二个脚本生成一个新进程/线程,该脚本将在启动前等待 1 秒.

I have a PHP script that I need to execute from inside another PHP webpage. However for the second one to run properly the first needs to have fully completed. Essentially I need the first page to spawn a new process/thread for the second script which will wait 1 second before starting.

执行包含会导致阻塞,从而阻止它工作,我无法让它开始使用 exec

Doing an include causes blocking which prevents it from working and I can't get it to start using exec

应该澄清一下.这些页面没有输出,也没有通过 Web 界面连接.所有页面都由来自另一台服务器的 POST 请求调用.

Should have clarified. These pages have no output and are not interfaced with through a web interface. All pages are called by POST requests from another server.


编辑 2:

解决方法:让请求页面的服务器在第一个页面返回1秒后直接向第二个页面发送请求.

Solution: make server requesting the page send a request directly to the second page 1 second after the first returns.

推荐答案

proc_open 是正确的选择,正如@ChristopherMorrissey 指出的那样.我想在这里详细说明一下,因为使用 proc_open 有一些不完全明显的警告.

proc_open is the correct choice, as @ChristopherMorrissey pointed out. I want to elaborate a little here, as there are some caveats to using proc_open that aren't entirely obviously.

在第一个代码示例中@http://php.net/manual/en/function.proc-open.php,它显示了整体用法,我会参考.

In the first code example @ http://php.net/manual/en/function.proc-open.php, it shows the overall usage and I will reference that.

第一个警告是管道.管道是 PHP 中的文件流,它们链接到子进程的 STDIN、STDOUT 和 STDERR.在该示例中,管道索引 0 表示从父 PHP 进程角度来看的文件流.如果父进程写入此流,它将显示为子进程的 STDIN 输入.

The first caveat is with the pipes. The pipes are file streams in PHP that link to STDIN, STDOUT and STDERR of the child process. In the example, pipe index 0 represents a file stream from the parent PHP processes perspective. If the parent process writes to this stream, it will appear as STDIN input to the child process.

在符合 POSIX 的操作系统上,进程的 STDIN 需要在进程终止之前关闭.从父进程在管道上调用 fclose 非常重要,否则你的子进程将被卡住.这是通过示例中的这一行完成的:

On POSIX compliant OSes, STDIN to a process needs to close before the process can terminate. Its very important to call fclose on the pipe from the parent, or your child process will be stuck. That is done with this line in the example:

fclose($pipes[0]);

另一个注意事项是检查子进程的退出代码.检查退出代码是确定子进程是否正确退出或出错的最佳方法.至少,您只需要知道子进程何时完成.检查这个和退出代码都是用 http://www.php.net/manual/en/function.proc-get-status.php

The other caveat is on checking the exit code of the child process. Checking the exit code is the best way to determine if the child process has exited correctly, or if it erred out. At the very least, you will need to just know when the child process has completed. Checking this and the exit code are both done with http://www.php.net/manual/en/function.proc-get-status.php

如果您想确保进程正确退出,您需要查看 proc_get_status 数组中返回的 exitcode 字段.请记住,此退出代码只会返回一次有效值.所有其他时间它将返回-1.因此,一次它返回 > -1 时,这就是您的实际退出代码.所以,第一次running == false,检查exitcode.

If you want to ensure the process exits correctly, you will need to look at the exitcode field returned in the array from proc_get_status. Keep in mind this exit code will only return a valid value once. All other times it will return -1. So, the one time it returns > -1, this is your actual exit code. So, the first time running == false, check exitcode.

我希望这会有所帮助.

这篇关于从另一个内部生成一个 PHP 脚本.无阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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