<?php
Swoole\Coroutine::create (callable $callback)
The function will be executed within the coroutine.
The array of params of the callback function
Create and execute the closure function in a coroutine. A coroutine is a light weight thread running within a Linux process.
Go
can be used to create new coroutine which is the short name of Swoole\Coroutine::create
. Co
is the short name of Swoole\Coroutine.
Coroutines must be executed within a coroutine context.
Co\run
can be used to create a context to execute coroutines.
A coroutine context is created for each callback function of Swoole Server
.
Since version 4.1.0, you can use any IO libraries using php_stream
within a coroutine context.
Supported libraries:
Not supported:
Mutliple corotuines created with go
are executed concurrently.
<?php
Co\run(function() {
go(function () {
co::sleep(3.0);
go(function () {
co::sleep(2.0);
echo "co[3] end\n";
});
echo "co[2] end\n";
});
co::sleep(1.0);
echo "co[1] end\n";
});
Each coroutine use 8KB memory stack to store variables by default in PHP 7.2+.
<?php
Co\run(function () {
co::sleep(0.5);
echo "hello";
go("test");
go([$object, "method"]);
});