1. Go to this page and download the library: Download venndev/vosaka-fourotines library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
venndev / vosaka-fourotines example snippets
use function vosaka\foroutines\main;
main(function () {
// Your async code here
});
use vosaka\foroutines\{Async, Delay, Dispatchers};
// Create and await a single async task
$result = Async::new(function () {
Delay::new(100);
return 42;
})->await();
// Run in a separate worker process (IO dispatcher)
$io = Async::new(function () {
return file_get_contents('data.txt');
}, Dispatchers::IO)->await();
use vosaka\foroutines\{Async, Delay};
$asyncA = Async::new(function () {
Delay::new(500);
return 42;
});
$asyncB = Async::new(function () {
Delay::new(800);
return 'hello';
});
$asyncC = Async::new(function () {
Delay::new(300);
return 100;
});
// All three run concurrently — total time ≈ 800ms, not 1600ms
[$a, $b, $c] = Async::awaitAll($asyncA, $asyncB, $asyncC);
// Also works with spread operator
$results = Async::awaitAll(...$arrayOfAsyncs);
use vosaka\foroutines\{WithTimeout, WithTimeoutOrNull, Delay};
// Throws RuntimeException if exceeded
$val = WithTimeout::new(2000, function () {
Delay::new(1000);
return 'ok';
});
// Returns null instead of throwing
$val = WithTimeoutOrNull::new(500, function () {
Delay::new(3000);
return 'too slow';
});
RunBlocking::new(function () {
Launch::new(fn() => print("A"));
Thread::await(); // Blocks here until "A" is printed
print("B"); // Always prints after "A"
});
use vosaka\foroutines\FiberPool;
// Adjust global pool size
FiberPool::setDefaultSize(20);
// Direct usage (zero-alloc reuse after first run)
$pool = new FiberPool(maxSize: 10);
$result = $pool->run(fn() => heavyComputation());
use vosaka\foroutines\actor\{Actor, Message, ActorSystem};
class GreeterActor extends Actor {
protected function receive(Message $msg): void {
echo "Hello, {$msg->payload}!\n";
}
}
main(function () {
RunBlocking::new(function () {
$system = ActorSystem::new()
->register(new GreeterActor('greeter'));
$system->startAll();
$system->send('greeter', Message::of('greet', 'World'));
Delay::new(100);
$system->stopAll();
});
});