简体   繁体   English

proc_open()运行一个进程使PHP等待它完成?

[英]proc_open() run a process make PHP wait it to finish?

I use XAMPP 1.7.7 on windows7.(PHP Version 5.3.8) I use proc_open() run a process and want to redirect to another web page, but PHP will wait until the process is finished. 我在Windows7上使用XAMPP 1.7.7。(PHP版本5.3.8)我使用proc_open()运行一个进程,并希望重定向到另一个网页,但是PHP将等待该进程完成。 I don't want the running process make my web to wait it. 我不希望正在运行的进程使我的网站等待它。 What should I do? 我该怎么办?

And I need pipes and the return value. 我需要管道和返回值。

What I need: A user submit something in page A,then the web will redirect to page B(and user can leave page B). 我需要什么:用户在页面A中提交内容,然后网络将重定向到页面B(用户可以离开页面B)。 At the same time some processes will be called , produce some results and update the database,so when the user refresh the page B,the right result will be show. 同时将调用某些过程,产生一些结果并更新数据库,因此,当用户刷新页面B时,将显示正确的结果。 What's more,the user can view the page B any time. 此外,用户可以随时查看页面B。 I notice that chris's comment on PHP Manual ,his method can run a process which is independent with PHP.But I don't know how to use pipes on the hide process or get the return value. 我注意到克里斯对PHP Manual的评论,他的方法可以运行一个独立于PHP的进程。但是我不知道如何在hide进程上使用管道或获取返回值。

And I have no idea on AJAX,I think the Gearman maybe work,but it's maybe a little complex. 而且我对AJAX还一无所知,我认为Gearman也许可以工作,但是可能有点复杂。

PHP is single threaded by design. PHP是单线程设计的。 There is no way to leave a php process running when the HTTP request has finished. HTTP请求完成后,无法让php进程继续运行。

Having said that, you could exploit AJAX to do what you want. 话虽如此,您可以利用AJAX做您想要的事情。 Instead of having one HTTP request, fire two requests at the same time. 而不是拥有一个HTTP请求,而是同时触发两个请求。 One of them will contain the long process (along with set_time_limit(0) ). 其中之一将包含较长的过程(以及set_time_limit(0) )。

There are a lot of different ways to do that. 有很多不同的方法可以做到这一点。 What I usually do is that: When I receive the initial request, I respond immediately with an HTML page that contains an automatic AJAX call to the second php file that contains the long process. 我通常要做的是:收到初始请求后,我立即响应一个HTML页面,该页面包含对包含较长过程的第二个php文件的自动AJAX调用。 So everybody is happy: the user sees immediate response and the long process can take its time as nobody is waiting. 因此,每个人都很高兴:用户看到了立即的响应,并且漫长的过程可能会花费很多时间,因为没有人在等待。

This should be done using a job queue like Gearman so that you can leave a worker running and then interrogate it for its status later from the page you redirect to. 应该使用诸如Gearman之类的作业队列来完成此操作,以便您可以让工作人员保持运行状态,然后稍后在您重定向到的页面上查询其状态。

To install Gearman on Windows please see this previous SO question and answers: How to configure or install GEARMAN in windows OS? 要在Windows上安装Gearman,请参阅此先前的SO问题和解答: 如何在Windows OS中配置或安装GEARMAN?

尝试查看您的问题是否通过以下方法得到了解决: http : //nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/

I have solved this by start a new PHP to implements my request. 我已经通过启动新的PHP来实现我的请求来解决此问题。 http://www.php.net/manual/en/function.proc-open.php#90584 http://www.php.net/manual/zh/function.proc-open.php#90584

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

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