加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
example1.php 856 Bytes
一键复制 编辑 原始数据 按行查看 历史
BigBoom 提交于 2017-08-01 14:23 . v1.0
<?php /*
Basic usage
for beginners: it is for command-line only execution. Webservers do not support threads.
*/
require_once("ThreadQueue.php");
// it is the function that will be called several times
function parallel_task($arg){
echo "task with parameter '$arg' starts\n";
sleep( rand(2,5) ); // wait for random seconds
echo "task with parameter '$arg' ENDS\n";
}
// create a queue instance with a callable function name
$TQ = new ThreadQueue("parallel_task");
// add tasks
$TQ->add("one");
$TQ->add("two");
$TQ->add("three");
$TQ->add("four");
$TQ->add("five");
// wait until all threads exit
while( count( $TQ->threads() ) ){ // there are existing processes in the background?
sleep(1); // optional
echo "waiting for all jobs done...\n";
$TQ->tick(); // mandatory!
}
echo "all process finished.\n";
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化