mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
Add signal and stdin examples
This commit is contained in:
parent
2a5600f027
commit
1b1ea5688c
15
examples/basic/signals.php
Normal file
15
examples/basic/signals.php
Normal file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
use Amp\Loop;
|
||||
|
||||
print "Press Ctrl+C to exit..." . PHP_EOL;
|
||||
|
||||
Loop::onSignal(SIGINT, function () {
|
||||
print "Caught SIGINT, exiting..." . PHP_EOL;
|
||||
exit(0);
|
||||
});
|
||||
|
||||
Loop::run();
|
21
examples/basic/stdin.php
Normal file
21
examples/basic/stdin.php
Normal file
@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
use Amp\Loop;
|
||||
|
||||
if (stream_set_blocking(STDIN, false) !== true) {
|
||||
fwrite(STDERR, "Unable to set STDIN to non-blocking" . PHP_EOL);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
print "Write something and hit enter" . PHP_EOL;
|
||||
|
||||
Loop::onReadable(STDIN, function ($watcher, $stream) {
|
||||
$chunk = fread($stream, 8192);
|
||||
|
||||
print "Read " . strlen($chunk) . " bytes" . PHP_EOL;
|
||||
});
|
||||
|
||||
Loop::run();
|
Loading…
Reference in New Issue
Block a user