2015-08-06 18:59:25 -05:00
|
|
|
#!/usr/bin/env php
|
2015-07-13 17:30:59 -05:00
|
|
|
<?php
|
2015-08-03 15:09:26 -05:00
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
2015-07-13 17:30:59 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
use Amp\Parallel\Threading\Thread;
|
2017-03-16 17:03:59 -05:00
|
|
|
use Amp\{ Loop, Pause };
|
2016-08-18 11:04:48 -05:00
|
|
|
|
2017-03-16 17:03:59 -05:00
|
|
|
Loop::run(function () {
|
2016-12-29 19:17:26 -06:00
|
|
|
$timer = Loop::repeat(1000, function () {
|
2016-08-18 11:04:48 -05:00
|
|
|
static $i;
|
|
|
|
$i = $i ? ++$i : 1;
|
|
|
|
print "Demonstrating how alive the parent is for the {$i}th time.\n";
|
2015-08-03 15:09:26 -05:00
|
|
|
});
|
2016-08-18 11:04:48 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Create a new child thread that does some blocking stuff.
|
|
|
|
$context = Thread::spawn(function () {
|
|
|
|
printf("\$this: %s\n", get_class($this));
|
|
|
|
|
|
|
|
printf("Received the following from parent: %s\n", yield $this->receive());
|
|
|
|
|
|
|
|
print "Sleeping for 3 seconds...\n";
|
|
|
|
sleep(3); // Blocking call in thread.
|
|
|
|
|
|
|
|
yield $this->send("Data sent from child.");
|
|
|
|
|
|
|
|
print "Sleeping for 2 seconds...\n";
|
|
|
|
sleep(2); // Blocking call in thread.
|
|
|
|
|
|
|
|
return 42;
|
|
|
|
});
|
|
|
|
|
|
|
|
print "Waiting 2 seconds to send start data...\n";
|
|
|
|
yield new Pause(2000);
|
|
|
|
|
|
|
|
yield $context->send("Start data");
|
|
|
|
|
|
|
|
printf("Received the following from child: %s\n", yield $context->receive());
|
|
|
|
printf("Thread ended with value %d!\n", yield $context->join());
|
|
|
|
} finally {
|
2016-12-29 19:17:26 -06:00
|
|
|
Loop::cancel($timer);
|
2016-08-18 11:04:48 -05:00
|
|
|
}
|
2017-03-16 17:03:59 -05:00
|
|
|
});
|