tasque/event-loop

v0.1.5 2024-08-21 02:33 UTC

This package is auto-updated.

Last update: 2024-08-21 02:34:57 UTC


README

Build Status

Run a ReactPHP event loop within a conventional PHP application using Tasque.

Why?

To allow periodic background tasks, such as sending keep-alive or heartbeat messages, to be performed in a traditional PHP environment where there would otherwise be no event loop.

Usage

Install this package with Composer:

$ composer install tasque/event-loop

Configure Nytris platform:

nytris.config.php

<?php

declare(strict_types=1);

use Nytris\Boot\BootConfig;
use Nytris\Boot\PlatformConfig;
use Tasque\EventLoop\TasqueEventLoopPackage;
use Tasque\TasquePackage;

$bootConfig = new BootConfig(new PlatformConfig(__DIR__ . '/var/cache/nytris/'));

$bootConfig->installPackage(new TasquePackage());
$bootConfig->installPackage(new TasqueEventLoopPackage());

return $bootConfig;

Setting a timer

Just use a standard ReactPHP timer, as long as the package is configured in nytris.config.php as above.

index.php

<?php

declare(strict_types=1);

use React\EventLoop\Loop;

require_once __DIR__ . '/vendor/autoload.php';

Loop::addPeriodicTimer(1, function () {
    print 'Timer elapsed' . PHP_EOL;
});

Awaiting a ReactPHP promise from a thread

<?php

declare(strict_types=1);

use Tasque\EventLoop\TasqueEventLoop;

/*
 * This will cause the current thread (which may be the main thread
 * as well as a background thread) to pause until the promise is resolved,
 * in which case the result will be returned, or rejected, in which case
 * the reason will be thrown.
 */
$result = TasqueEventLoop::await($promise);

See also