PHP中使用協(xié)同程序?qū)崿F(xiàn)合作多任務(wù)第1/2頁(4)_PHP教程
推薦:PHP中使用協(xié)同程序?qū)崿F(xiàn)合作多任務(wù)PHP5.5一個(gè)比較好的新功能是實(shí)現(xiàn)對生成器和協(xié)同程序的支持。對于生成器,PHP的文檔和各種其他的博客文章(就像這一個(gè)或這一個(gè))已經(jīng)有了非常詳細(xì)的講解。協(xié)同程序相對受到的關(guān)注就少了,所以協(xié)同程序雖然有很強(qiáng)大的功能但也很難被知曉,解釋起來也比較困難。 這篇文章
if ($retval instanceof SystemCall) {
$retval($task, $this);
continue;
}
if ($task->isFinished()) {
unset($this->taskMap[$task->getTaskId()]);
} else {
$this->schedule($task);
}
}
}
第一個(gè)系統(tǒng)調(diào)用除了返回任務(wù)ID外什么都沒有做:
復(fù)制代碼 代碼如下:<?php
function getTaskId() {
return new SystemCall(function(Task $task, Scheduler $scheduler) {
$task->setSendValue($task->getTaskId());
$scheduler->schedule($task);
});
}
這個(gè)函數(shù)確實(shí)設(shè)置任務(wù)id為下一次發(fā)送的值,并再次調(diào)度了這個(gè)任務(wù)。由于使用了系統(tǒng)調(diào)用,所以調(diào)度器不能自動(dòng)調(diào)用任務(wù),我們需要手工調(diào)度任務(wù)(稍后你將明白為什么這么做)。要使用這個(gè)新的系統(tǒng)調(diào)用的話,我們要重新編寫以前的例子:
復(fù)制代碼 代碼如下:<?php
function task($max) {
$tid = (yield getTaskId()); // <-- here's the syscall!
for ($i = 1; $i <= $max; ++$i) {
echo "This is task $tid iteration $i.\n";
yield;
}
}
$scheduler = new Scheduler;
$scheduler->newTask(task(10));
$scheduler->newTask(task(5));
$scheduler->run();
這段代碼將給出與前一個(gè)例子相同的輸出。注意系統(tǒng)調(diào)用同其他任何調(diào)用一樣正常地運(yùn)行,不過預(yù)先增加了yield。要?jiǎng)?chuàng)建新的任務(wù),然后再殺死它們的話,需要兩個(gè)以上的系統(tǒng)調(diào)用:
復(fù)制代碼 代碼如下:<?php
function newTask(Generator $coroutine) {
return new SystemCall(
function(Task $task, Scheduler $scheduler) use ($coroutine) {
$task->setSendValue($scheduler->newTask($coroutine));
$scheduler->schedule($task);
}
);
}
function killTask($tid) {
return new SystemCall(
function(Task $task, Scheduler $scheduler) use ($tid) {
$task->setSendValue($scheduler->killTask($tid));
$scheduler->schedule($task);
}
);
}
killTask函數(shù)需要在調(diào)度器里增加一個(gè)方法:
復(fù)制代碼 代碼如下:<?php
public function killTask($tid) {
if (!isset($this->taskMap[$tid])) {
return false;
}
unset($this->taskMap[$tid]);
// This is a bit ugly and could be optimized so it does not have to walk the queue,
// but assuming that killing tasks is rather rare I won't bother with it now
foreach ($this->taskQueue as $i => $task) {
if ($task->getTaskId() === $tid) {
unset($this->taskQueue[$i]);
break;
}
}
return true;
}
用來測試新功能的微腳本:
復(fù)制代碼 代碼如下:<?php
function childTask() {
$tid = (yield getTaskId());
while (true) {
echo "Child task $tid still alive!\n";
yield;
}
}
function task() {
$tid = (yield getTaskId());
$childTid = (yield newTask(childTask()));
for ($i = 1; $i <= 6; ++$i) {
echo "Parent task $tid iteration $i.\n";
yield;
if ($i == 3) yield killTask($childTid);
}
}
$scheduler = new Scheduler;
$scheduler->newTask(task());
$scheduler->run();
分享:php修改NetBeans默認(rèn)字體的大小在Netbeans中由于使用了Swing進(jìn)行開發(fā),所以其中界面的字體也是由Java虛擬機(jī)進(jìn)行配置而不是隨操作系統(tǒng)的。在安裝完Netbeans后默認(rèn)的字體大小是11px。而在Windows下的宋體最小支持12px。所以字體為11px就已經(jīng)無法完整顯示了。 簡單的解決辦法就是將字體改大一點(diǎn)。詳細(xì)的
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-PHP中使用協(xié)同程序?qū)崿F(xiàn)合作多任務(wù)第1/2頁(4)
。