我不知道,但你可以嘗試像
<?php
include 'included.php';
$instances = array();
while(true)
{
foreach ($instances as $key => $instance) {
if ($result = stream_get_contents($instance)) {
pclose($instance);
unset($instances[$key]);
$o = unserialize($result);
// do something with the object
}
}
$instances[] = popen('php /path/to/include.php', 'r');
}
?>
<!-- in include.php -->
<?php
include 'included.php';
echo serialize(new tester());
?>
或者你可以將所有的邏輯爲「include.php」所以主腳本不關心結果的一切,你將需要返回主要腳本知道它可能會關閉進程句柄。
在這裏,我們在推出單獨的進程腳本與popen('php /path/to/include.php', 'r');
,並得到一個可讀的管給它(這樣我們就可以讀了什麼過程輸出)。當在單獨的過程被成功地創建的對象,我們序列化並輸出結果。回到每個迭代的主腳本中,我們檢查已經打開的實例,如果它們中的一些返回某些內容 - 我們將這些內容視爲序列化對象,將其反序列化,然後使用它處理任何我們想要的內容;那麼在完成我們想要的任務後,我們關閉該進程並將其從打開的實例列表中刪除。其實,如果在你的代碼while(true)
並沒有真正運行下去,而是直到某些條件得到滿足 - 這將是加工環節轉移到不同的週期,在初始化之後是一個好主意......就像
while($condition)
{
$instances[] = popen('php /path/to/include.php', 'r');
}
while (!empty($instances)) {
foreach ($instances as $key => $instance) {
if ($result = stream_get_contents($instance)) {
pclose($instance);
unset($instances[$key]);
$o = unserialize($result);
// do something with the object
}
}
}
「等待...無需停止主循環的流程」可以通過使用過程控制產卵單獨的進程(PHP腳本的實例)。那是你要的嗎?也許是 –
。如果妳能詳細瞭解了有關多一點 – kritya
[fork調用(http://php.net/manual/ru/function.pcntl-fork.php)和pcntl_ *功能,但它不會PHP是工作時用作Apache模塊。注意叉子炸彈。 – sanmai