moebius/coroutine

Easy to use coroutines for PHP.

1.0.102 2022-06-22 15:30 UTC

README

True "green threads" (coroutines) for PHP 8.1. No plugins needed. Coroutines are like multitasking, but without many of the subtle problems that come from threads.

Compatability

Moebius runs other event loops cooperatively; if you are using React, Moebius will run the React Event loop, if you are using Amp, Moebius will run the Amp event loop.

TIP! To make compatible event-loop based applications, you can implement against the moebius/loop implementation yourself.

Essentials

A coroutine is a function which runs in parallel with other code in your application. You can work with coroutines the same way you work with promises in frameworks like React or Amp. In fact, you can use most promise based libraries with Moebius.

Conceals the promises

The main purpose of moebius is to conceal the existence of promises; you should not have to think about coroutines being a part of your application. The only time you need to think about coroutines, is when you need to do multiple things in parallell.

Moebius allows your application to handle multiple requests in parallel, but your program flow should not have to worry about that.

A Coroutine is a Promise

When you create a coroutine, you get a promise about a future result. That future result can be accessed via the then() method, just like any other promise object you're used to.

<?php
use Moebius\Coroutine as Co;

$coroutine = Co::go(function() {
    Co::sleep(10);
    return true;
});

$coroutine->then(function() {
    echo "Done\n";
});

A Promise is a Coroutine

<?php
use Moebius\Coroutine as Co;
use GuzzleHttp\Client;

function get(string $url) {
    $client = new Client();
    echo "Connecting to '$url'\n";
    $result = $client->getAsync($url);
    echo "Got response from '$url'\n";
}

$google = Co::go(get(...), 'https://www.google.com');
$bing = Co::go(get(...), 'https://www.bing.com');
$ddg = Co::go(get(...), 'https://www.duckduckgo.com');



## Examples

You can find complete examples in the `examples/` folder. Here is a trivial example
that reads lines from multiple files concurrently: