Skip to content

Latest commit

 

History

History
61 lines (41 loc) · 1.79 KB

2.Events.md

File metadata and controls

61 lines (41 loc) · 1.79 KB

Events transfer from thread


  1. Events from the thread
  2. Events from the thread pool

1 - Events from the thread

During task execution "thread" can report something to parent. To do this, each thread is the event dispatcher.

Sending event can be made with the trigger method. You can send various data with the event. Such as the status of work, or any other information.

Subscribe to events in the parent process with the bind method. You can also specify an additional argument with the callback and the event name when subscribing. Its value will be passed to the callback as the third argument each time it triggers.

class ExampleThread extends Thread
{
	const EV_NAME = 'example_name';
	function process()
	{
		// ...
		$this->trigger(self::EV_NAME, $event_data);
		// ...
	}
}
$thread = new ExampleThread();
$thread->bind(ExampleThread::EV_NAME, function($event_name, $event_data, $additional_arg) {
	// ...
}, $additional_arg);
$thread->wait()->run()->wait();

An example of events generation from the "thread" can be found among other examples.

2 - Events from the thread pool

Receiving events from from the thread pool is as simple as a from the single thread. Just subscribe on the pool. In this case, you will have a second argument for the callback - ID of the thread that sent the event.

$cb = function($event_name, $threadId, $event_data, $additional_arg) {
	// ...
};
$pool->bind(ExampleThread::EV_NAME, $cb, $additional_arg);

Within the thread method call is the same - trigger.

$this->trigger(self::EV_NAME, $event_data);