开发者

Does PHP have job control like bash does?

does PHP support something like ampersand in bash (forking)? Let's say I wanted to use cURL on 2 web pages concurrently, so script doesn't have to wait before fi开发者_StackOverflow中文版rst cURL command finnishes, how could one achieve that in PHP? Something like this in bash:

 curl www.google.com &
 curl www.yahoo.com &
 wait


Does PHP support something like ampersand in bash (forking)?
No. See the other answers, though I do point out that the PCNTL extension is UNIX only.

...how could one achieve that in PHP?*
cURL supports running multiple downloads concurrently.


You can use popen or proc_open to open a process and let it run in the background, but there's no language support for background operations like there is for bash (which was made to run tasks, anyways; PHP was made for scripting stuff).

Using proc_open, you can then use proc_get_status to know when the processes are terminated. I'm afraid there's no wait equivalent.


You can write asynchronous methods if you really want to, but it's a lot of work, and probably a terrible design idea if you're using PHP.


Some kind of queuing mechanism is often preferable. Gearman is an open source queuing mechanism you can use. I also have a blog post on the Zend Server Job Queue that talks about running tasks asynchronously Do you queue? Introduction to the Zend Server Job Queue.

You could also use something like the Zend Framework Queuing classes to implement some of the asynchronous work. Zend_Queue

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜