Subscribe to PHP Freaks RSS

Sergey Zhuk: Understanding ReactPHP Event Loop Ticks

syndicated from www.phpdeveloper.org on September 29, 2017

Sergey Zhuk has a new post to his site that hopes to help you better understand "ticks" in ReactPHP. Ticks are a feature of the tool that are used to track when a process or queue has been executed.

Tick is one loop iteration where every callback in the queues has been executed synchronously and in order. ReactPHP event loop implementation has two main methods to work with ticks: nextTick [and] futureTick.

Both methods can be used to schedule a callback to be invoked on a future iteration of the event loop. When being executed a callback receives an instance of the event loop as an argument. But then what’s the difference between next and future ticks? Let’s figure this out.

He then starts in talking about the difference between "future" and "next" ticks, illustrating with a simple "stream select" loop. He then shows how to work with the tick queue for both the future and next ticks and what the result is of each function call inside them. He includes the output of his sample scripts and what happens if a few things change.

Consider a tick as one loop iteration where every callback in the queues has been executed synchronously and in order. That means that a tick could be long, it could be short, but we want it to be as short as possible. So, don’t place long-running tasks in callbacks, because they will block the loop. When a tick a being stretched out, the event loop won’t be able to check the events, which means losing performance for your asynchronous code.