Skip to content

How return data from the Swoole\Coroutine\Scheduler? #4462

Answered by twose
AkioSarkiz asked this question in Q&A
Discussion options

You must be logged in to vote

example

$data = [];
$scheduler = new Scheduler();
$scheduler->set(['max_coroutine' => $this->maxCountCoroutines]);
$scheduler->add(fn(string $token) => $data[] = $this->getFromFirstServer($token), $token);
$scheduler->add(fn(string $token) => $data[] = $this->getFromSecondServer($token), $token);
$scheduler->start();

var_dump($data);

I think I see what you mean, but you can only get data by reference vars or global vars for the time being...

Coroutine\run(function () use (&$data) {
    $channel = new Coroutine\Channel(1);

    go(function ($a, $b) use ($channel) {
        Coroutine::sleep(1);
        echo assert($a == 'hello') . PHP_EOL;
        echo assert($b == 12345) . PHP_EOL;
   …

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@AkioSarkiz
Comment options

@AkioSarkiz
Comment options

@sy-records
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by AkioSarkiz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants