moebius / coroutine
Easy to use coroutines for PHP.
Requires
- moebius/common: >=1.0
- moebius/loop: >=1.0.100
- moebius/promise: >=1.0.100
Requires (Dev)
- charm/testing: >=1
Suggests
- moebius/async-file: Automatically and transparently makes blocking file system operations non-blocking
- dev-master
- 1.0.102
- 1.0.101
- 1.0.100
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5-RC14
- 1.0.5-RC13
- 1.0.5-RC12
- 1.0.5-RC11
- 1.0.5-RC10
- 1.0.5-RC9
- 1.0.5-RC8
- 1.0.5-RC7
- 1.0.5-RC6
- 1.0.5-RC5
- 1.0.5-RC4
- 1.0.5-RC3
- 1.0.5-RC2
- 1.0.5-RC1
- 1.0.5-beta3
- 1.0.5-beta2
- 1.0.5-beta
- 1.0.4-beta
- 1.0.3-beta
- 1.0.2-alpha
- 1.0.1-alpha
- 1.0.0-alpha
- 0.0.10-beta
- 0.0.9-beta
- 0.0.8-beta
- 0.0.7-beta
- 0.0.6-beta
- 0.0.5-beta
- 0.0.4-beta
- 0.0.3-beta
- 0.0.2-beta
- 0.0.1-alpha
This package is auto-updated.
Last update: 2024-11-28 06:07:31 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: