mirror of
https://github.com/danog/amp.git
synced 2024-11-26 20:15:00 +01:00
Add coroutine concurrency example
This commit is contained in:
parent
2a4ae8474b
commit
31f021e59a
33
examples/coroutines/concurrency.php
Normal file
33
examples/coroutines/concurrency.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
#!/usr/bin/env php
|
||||
|
||||
require __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
use Amp\Delayed;
|
||||
use Amp\Loop;
|
||||
use function Amp\asyncCall;
|
||||
|
||||
// Shows how two for loops are executed concurrently.
|
||||
|
||||
// Note that the first two items are printed _before_ the Loop::run()
|
||||
// as they're executed immediately and do not register any timers or defers.
|
||||
|
||||
asyncCall(function () {
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
print "1 - " . $i . PHP_EOL;
|
||||
yield new Delayed(1000);
|
||||
}
|
||||
});
|
||||
|
||||
asyncCall(function () {
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
print "2 - " . $i . PHP_EOL;
|
||||
yield new Delayed(400);
|
||||
}
|
||||
});
|
||||
|
||||
print "-- before Loop::run()" . PHP_EOL;
|
||||
|
||||
Loop::run();
|
||||
|
||||
print "-- after Loop::run()" . PHP_EOL;
|
Loading…
Reference in New Issue
Block a user