tasque/tasque

Maintainers

Details

github.com/nytris/tasque

Source

Issues

Installs: 143

Dependents: 4

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:project

v0.1.4 2024-02-06 07:15 UTC

This package is auto-updated.

Last update: 2024-05-06 07:54:53 UTC


README

Build Status

[EXPERIMENTAL] Run PHP background green threads concurrently.

Note that these are not true threads: instead they are "green threads" as they still run in the single main PHP process' primary OS thread, inside a separate Fiber for each, being interrupted by the Tasque scheduler when it is time to context switch either to the next background thread or back to the main thread.

This is why we use the term "concurrent" rather than "parallel".

Why?

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

Demos

  • See the Tasque demo for an example of how Tasque is used to start multiple threads.
  • See the Tasque EventLoop demo to see how Tasque EventLoop can be used to run a ReactPHP event loop inside one of those threads, concurrently with the main thread (and any other Tasque background threads).

Usage

Install this package with Composer:

$ composer install tasque/tasque

Configure Nytris platform:

nytris.config.php

<?php

declare(strict_types=1);

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

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

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

return $bootConfig;

Starting a thread

index.php

<?php

declare(strict_types=1);

use Tasque\Tasque;

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

$tasque = new Tasque();

$thread = $tasque->createThread(
    function () {
        /*
         * Run your background thread logic here.
         *
         * Note that this should be outside of the entrypoint script for your application,
         * so that PHP Code Shift can transpile it with the tock calls required by Tasque.
         */
    }
);
$thread->start();

// Wait for the background thread to complete and capture its result.
$thread->join();
$backgroundThreadResult = $thread->getReturn();

print 'Background thread result: "' . $backgroundThreadResult . '"';

Limitations

  • Threads can only be handled once Tasque itself has been able to initialise.

See also