tasque / event-loop
Installs: 2 700
Dependents: 5
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- php: >=8.1
- react/event-loop: ^1.5
- react/promise: ^3.2 || ^2.11
- tasque/tasque: ^0.1
Requires (Dev)
- mockery/mockery: 1.6.11
- phpstan/phpstan: ^1.10
- phpstan/phpstan-mockery: ^1.1
- phpunit/phpunit: ^10.2
README
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);