WyriHaximus · GitHub

Conversation

@WyriHaximus

WyriHaximus

public function on($signal, callable $listener)
{
if (!$this->enabled) {
return;
}

if ($this->timer === null) {
$this->timer = $this->loop->addPeriodicTimer(0.1, function () {
public function testSignal()
{
if (!extension_loaded('pcntl')) {
$this->markTestSkipped('Signal test skipped because PCNTL extension isn\'t loaded.');

trowski

$this->loop = new EventLoop();
$this->futureTickQueue = new FutureTickQueue();
$this->timerEvents = new SplObjectStorage();
$this->signals = new Pcntl($this);

clue

be notified about OS signals. This is useful to catch user interrupt signals or
shutdown signals from tools like `supervisor` or `systemd`.

The
* @param callable $listener
*
* @return void
*/
* @param callable $listener
*
* @return void
*/
public function on($signal, callable $listener)
{
if (!$this->enabled) {
return;
}

if ($this->timer === null) {
$this->timer = $this->loop->addPeriodicTimer(0.1, function () {
});

$this->loop->futureTick(function () {
posix_kill(posix_getpid(), SIGUSR1);

clue

$this->signalEvents[$signal]->add();
});
$this->signals->on('off', function ($signal) {
$this->futureTick(function () use ($signal) {
"require": {
"php": ">=5.4.0"
"php": ">=5.4.0",
"evenement/evenement": "^3.0||^2.0"

**Note: A listener can be added more then once to the same signal and it will be called
for each time it is added. But on [`removeSignal`](#removeSignal) it is removed for all
the times it has been added.
"ext-event": "~1.0",
"ext-libev": "*"
"ext-libev": "*",
"ext-pcntl": "For signals support"
* @param callable $listener
*
* @throws UnsupportedFeatureException when signals
* aren't supported by the loop, e.g. when required
$available = $this->streamSelect($read, $write, $timeout);
if ($this->pcntl) {
\pcntl_signal_dispatch();
}

kelunik

if (isset($this->signals[$signal]) && \count($this->signals[$signal]) === 0) {
unset($this->signals[$signal]);
$this->emit('off', [$signal]);
}

kelunik

$this->signals->on('on', function ($signal) {
\pcntl_signal($signal, function ($signal) {
$this->signals->call($signal);
});

jsor

jsor approved these changes Aug 29, 2017

Open

clue


$loop = React\EventLoop\Factory::create();

$loop->onSignal(SIGINT, $func = function ($signal) use ($loop, &$func) {
$this->signalEvents[$signal]->add();
},
function ($signal) {
$this->futureTick(function () use ($signal) {
* @param callable $listener
*
* @throws UnsupportedFeatureException when signals
* aren't supported by the loop, e.g. when required
{
return new self('Event loop feature "' . $feature . '" isn\'t supported by the "' . $loopName . '" event loop');
}
}

final class SignalsHandlerTest extends TestCase
{
public function testEmittedEventsAndCallHandling()

kelunik


if (!defined('SIGUSR2')) {
define('SIGUSR2', 2);
}

kelunik

$loop->stop();
});

// This timer is here to keep the loop alive, signaling an-sich doesn't do that

kelunik

{
public static function create($feature, $loopName)
{
return new self('Event loop feature "' . $feature . '" isn\'t supported by the "' . $loopName . '" event loop');

@WyriHaximus

clue


### addSignal()

**Note: Signaling is only available on Unix-like platform, Windows isn't supported.**
See also [example #4](examples).

**Note: A listener can only be added once to the same signal, any attempts to add it
more then once will be ignored.**
$this->loop->addTimer(0.1, function () {});

$this->assertRunFasterThan(0.2);
}
$loop->stop();
});

// This timer is here to keep the loop alive, signaling by itself doesn't do that

@WyriHaximus

clue

$loop->addSignal(SIGINT, $func = function ($signal) use ($loop, &$func) {
echo 'Signal: ', (string)$signal, PHP_EOL;
$loop->removeSignal(SIGINT, $func);
$loop->stop();
{
if (count($this->signals) == 0 && $this->timer === null) {
$this->timer = $this->loop->addPeriodicTimer(1, function () {});
}

WyriHaximus added a commit to WyriHaximus-labs/event-loop that referenced this pull request

Nov 4, 2017

WyriHaximus added a commit to WyriHaximus-labs/event-loop that referenced this pull request

Nov 4, 2017

clue

clue approved these changes Nov 10, 2017

@clue clue changed the title OS signaling Add support for signal handling

Nov 10, 2017

@WyriHaximus

@WyriHaximus

@clue clue mentioned this pull request

Nov 11, 2017

Closed

This was referenced

Feb 6, 2018

Merged

Merged

@cebe cebe mentioned this pull request

May 17, 2018

Closed

@TBK TBK mentioned this pull request

Jun 11, 2018

Closed

@clue clue mentioned this pull request

Feb 6, 2019

Closed

Read the original on github.com ↗