1. Go to this page and download the library: Download yurunsoft/xcoroutine library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yurunsoft / xcoroutine example snippets
foreach(range(1,10) as $i){
echo $i." ";
}
echo "<br>\n";
function xrange($a,$b,$c=1){
for($i = $a; $a <= $b; $i+=$c){
yield $i;
}
}
foreach(xrange(1,10) as $i){
echo $i." ";
}
tine\Task;
function TaskExample(){
while(true){
$text = (yield);
echo "TaskRecv $text <br />\n";
}
}
$Task = new Task(TaskExample());
$Task->run();
$Task->run("A Test Text 1");
$Task->setSendValue("Another Test Text");
$Task->run();
tine\Scheduler;
function TaskExample($num){
for($i=0; $i<10; ++$i){
echo "Task$num RUN $i <br>\n";
yield;
}
}
$scheduler = new Scheduler();
$scheduler->newTask(TaskExample(1));
$scheduler->newTask(TaskExample(2));
$scheduler->newTask(TaskExample(3));
$scheduler->run();
tine\Scheduler;
function t1(){
for($i=0; $i<10; ++$i){
echo "t1 RUN $i <br>\n";
yield;
}
}
function t2(){
for($i=0; $i<10; ++$i){
echo "t2 RUN $i <br>\n";
yield;
}
}
$scheduler = new Scheduler();
$scheduler->newTask(t1());
$scheduler->newTask(t2());
$scheduler->run();
tine\Scheduler;
use Coroutine\SystemCall;
function TaskExample($num){
//获取TaskId
$tid = (yield SystemCall::getTaskId());
for($i=0; $i<10; ++$i){
echo "Task$num $tid RUN $i <br>\n";
if($i == 3){
yield SystemCall::killTask(1);
}
if($i == 5){
yield SystemCall::newTask(Task2());
}
yield;
}
}
function Task2(){
$tid = (yield SystemCall::getTaskId());
for($i=0; $i<3; ++$i){
echo "Another Task $tid RUN $i <br>\n";
}
}
$scheduler = new Scheduler();
$scheduler->newTask(TaskExample(1));
$scheduler->newTask(TaskExample(2));
$scheduler->newTask(TaskExample(3));
$scheduler->run();