Skip to content

Commit c40a24e

Browse files
authored
Update example wsab.php
Use one channel
1 parent 1aa86b8 commit c40a24e

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

examples/async/wsab.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,10 @@ function microtime_float($usefloat = false)
578578
$connect_fail++;
579579
}
580580
};
581-
$channelnum = 10;
582-
for ($j=0; $j<=$channelnum; $j++) {
583-
$channel[$j] = new \Phalcon\Async\Channel($concurrency);
581+
582+
$channel = new \Phalcon\Async\Channel($concurrency);
583+
584+
for ($j=0; $j<=$concurrency; $j++) {
584585
$tasks[] = \Phalcon\Async\Task::asyncWithContext($context, static function (iterable $it) use ($context, $work, &$total_request, &$connect_success, &$connect_fail, &$send_success, &$send_fail, &$recv_success, &$recv_fail, &$min_request_time, &$max_request_time) {
585586
foreach ($it as $url) {
586587
if ($url == 'close') {
@@ -591,35 +592,29 @@ function microtime_float($usefloat = false)
591592

592593
}
593594

594-
}, $channel[$j]->getIterator());
595+
}, $channel->getIterator());
595596
}
596597

597598
$begin_time = microtime_float(true);
598599

599600
$n = 0;
600601
$shownum = ceil($requests/10);
601602
for ($i=0; $i<$requests; $i++) {
602-
$j = $i%$channelnum;
603-
$channel[$j]->send($url);
604-
$c = ceil($i/$shownum);
605-
if ($c > $n) {
603+
$channel->send($url);
604+
$c = floor($i/$shownum);
605+
if ($c > $n || $i == ($requests-1)) {
606606
echo 'Completed '.$i.' requests'.PHP_EOL;
607607
$n = $c;
608-
usleep(10);
609608
}
610609
}
611610

612-
for ($j=0; $j<=$channelnum; $j++) {
613-
$channel[$j]->send('close');
614-
}
611+
$channel->send('close');
615612

616613
foreach ($tasks as $t) {
617614
\Phalcon\Async\Task::await($t);
618615
}
619616

620-
for ($j=0; $j<=$channelnum; $j++) {
621-
$channel[$j]->close();
622-
}
617+
$channel->close();
623618

624619
$finish_time = microtime_float(true);
625620
$total_time = ($finish_time - $begin_time);

0 commit comments

Comments
 (0)