PHP中使用協(xié)同程序?qū)崿F(xiàn)合作多任務(wù)(4)_PHP教程
推薦:php修改NetBeans默認(rèn)字體的大小在Netbeans中由于使用了Swing進(jìn)行開(kāi)發(fā),所以其中界面的字體也是由Java虛擬機(jī)進(jìn)行配置而不是隨操作系統(tǒng)的。在安裝完Netbeans后默認(rèn)的字體大小是11px。而在Windows下的宋體最小支持12px。所以字體為11px就已經(jīng)無(wú)法完整顯示了。 簡(jiǎn)單的解決辦法就是將字體改大一點(diǎn)。詳細(xì)的
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外什么都沒(méi)有做:
復(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)用的話,我們要重新編寫(xiě)以前的例子:
復(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)行,不過(guò)預(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;
}
用來(lái)測(cè)試新功能的微腳本:
復(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刪除數(shù)組中特定元素的兩種方法這篇文章介紹了PHP中刪除數(shù)組中特定元素的兩種方法,有需要的朋友可以參考一下 方法一: 復(fù)制代碼 代碼如下: ?php $arr1 = array(1,3, 5,7,8); $key = array_search(3, $arr1); if ($key !== false) array_splice($arr1, $key, 1); var_dump($arr1); ? 輸出: array(4)
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問(wèn)控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語(yǔ)言構(gòu)造器介紹
- php/js獲取客戶(hù)端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語(yǔ)言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- PHP CLI模式下的多進(jìn)程應(yīng)用分析
- 如何用PHP和mysql創(chuàng)建一個(gè)ShoutBox
- 《PHP設(shè)計(jì)模式介紹》第十一章 代理模式
- 解讀PHP開(kāi)發(fā)人員應(yīng)熟悉的五個(gè)概念
- PHP應(yīng)用:PHP在linxu下的安裝與配置
- 用 PHP 使 Web 數(shù)據(jù)分析進(jìn)入更高境界
- 解讀Windows環(huán)境PHP的session為何不能正常使用
- php簡(jiǎn)單縮略圖類(lèi)|image.class.php
- Windows2003+IIS7 Express使用FastCgi運(yùn)行php
- PHP應(yīng)用程序加速探索之簡(jiǎn)介
- 相關(guān)鏈接:
- 教程說(shuō)明:
PHP教程-PHP中使用協(xié)同程序?qū)崿F(xiàn)合作多任務(wù)(4)
。