Loop Component
September 23, 2016 ยท View on GitHub
Introduction
Loop is a component that provides abstraction layer for writing asynchronous code in PHP on single thread or process with usage of single or multiple loop backgrounds.
Features
Loop features:
Concepts
This section contains terminology, useful concepts and definitions, that might be helpful to learn to fully understand this component purpose.
Event-Loop
The event loop is a central part of event-driven application. It schedules listeners, runs timers, handles signals, and polls streams for pending reads and available writes.
Examples
This section contains examples and patterns that can be used with described component.
Creating Loop
To create loop you have to pass to its constructor one of models, that can be found in Kraken\Loop\Model namespace.
$loop = new Loop(new SelectLoop);
Queuing
To queue any callbacks use onTick method.
$loop->onTick(function() {
// this will be executed as soon as possible!
});
For more control consider using onBeforeTick or onAfterTick methods.
Using Timers
To create timers you can use addTimer method.
$loop->addTimer(0.5, function() {
// this will be executed after half of a second
});
To create periodic timers, you addPeriodicTimer instead.
$loop->addPeriodicTimer(1.0, function() {
// this will be executed every each one second
});
Using Streams
To read data from streams use:
$loop->addReadStream($resource, function($resource) {
// any reading operation should be done in this section
});
To write data to streams use:
$loop->addWriteStream($resource, function($resource) {
// any writing operation should be done in this section
});
Catching Throwables
To catch throwable throw by loop, use try-catch block. Kraken Framework uses this kind of structure for reacting to loop problems:
while ($shouldProceed)
{
try
{
$loop->start();
}
catch (\Throwable $ex)
{
$shouldProceed = reactToProblem($ex);
}
}
Importing & Exporting
Importing and/or exporting the interal loop state might be done between two loop instances.
$loop->export($loopBackup);
$loop->import($loopBackup);
Using React Bridge
To create ReactPHP loop using existing Kraken loop, use:
$reactLoop = new \Kraken\Loop\Bridge\React\ReactLoop($krakenLoop);
Important Classes & Interfaces
This section contains list of most important classes and interfaces shipped with this component. It does not include all classes and interface.
Loop
class Loop implements LoopExtendedInterface
Loop is an abstraction layer for any of models existing in Kraken\Loop\Model namespace.
LoopInterface
interface LoopInterface
LoopExtendedInterface
interface LoopExtendedInterface extends LoopInterface
Important Directories
This section contains list of most important directories existing inside of component. It does not include all directories.
Bridge
This folder contains all bridges allowing for creation of loops compatible with other loop-based systems or components.
Model
This folder contains all models available to be injected to Kraken\Loop\Loop instance.