/** * @depends testDependency * * @return EventBase */ public function testBaseInit() { $base = new EventBase(); $this->assertTrue(is_resource($base->getResource()), 'Invalid event base resource.'); $this->assertEquals(1, $base->loop()); $base->free(); return $base; }
* Event break the base loop on limit * * @param EventInterface $event */ public function basic_timed_event_03(EventInterface $event) { $this->counter++; echo sprintf("Loop breaking persistent event call %s. Timeout: %s.\n", $event->getName(), $event->getTimeout()); if ($this->counter === $this->limit) { $event->getBase()->loopBreak(); echo sprintf("Call limit exceed for event %s. Loop break.\n", $event->getName()); } } } // Initialize event handler $eventHandler = new BasicTimedEventHandler(3); // Initialize event base $base = new EventBase(); // Map of events $map = array('basic_timed_event_01' => array('timeout' => 1800000, 'persist' => false), 'basic_timed_event_02' => array('timeout' => 1300000, 'persist' => true), 'basic_timed_event_03' => array('timeout' => 3000000, 'persist' => true)); /** * Prepare and enable events * @var EventTimer $event */ foreach ($map as $name => $arguments) { $event = new EventTimer($base, $name); $event->prepare(array($eventHandler, $name), array(), $arguments['persist'])->setTimeout($arguments['timeout'])->enable(); } // Base loop while breaks $base->loop();
// Initialize event base $base = new EventBase(); // Create map of events $map = array(SIGUSR1 => 'basic_sigusr1_event', SIGINT => 'basic_sigint_event', SIGTERM => 'basic_sigterm_event'); /** * Prepare and enable events * @var Event $event */ foreach ($map as $signal => $name) { $event = new Event($base, $name); $event->prepare($signal, EV_SIGNAL | EV_PERSIST, array($eventHandler, $name), array($signal))->enable(); } // Base infinite loop while (true) { // Set loop to non block $base->loop(EVLOOP_NONBLOCK); // Sleep 3 seconds usleep(3000000); /** * @link http://en.wikipedia.org/wiki/Kill_(command) */ // Next we must send the signals, just uncomment lines with posix functions if you have pisix extension. // Also we can send signal from console manualy using kill command, but first need to find out php process pid // $ ps axu |grep php // and kill or // $ kill -s USR1 $(ps axu |grep '[b]asic_signal_event.php' |awk '{print $2}') // $ kill $(ps axu |grep '[b]asic_signal_event.php' |awk '{print $2}') // or just hit ctrl+c // Send SIGUSR1 signal // posix_kill(posix_getpid(), SIGUSR1); // Send SIGTERM signal