All Projects → leocavalcante → swoole-futures

leocavalcante / swoole-futures

Licence: MIT License
⏳ Futures, Streams & Async/Await for PHP's Swoole asynchronous run-time.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to swoole-futures

swift-futures
Demand-driven asynchronous programming in Swift
Stars: ✭ 32 (-68%)
Mutual labels:  concurrency, streams, futures
mux-stream
(De)multiplex asynchronous streams
Stars: ✭ 34 (-66%)
Mutual labels:  concurrency, async-await, futures
async-oneshot
A fast, small, full-featured, no-std compatible oneshot channel
Stars: ✭ 55 (-45%)
Mutual labels:  concurrency, async-await, futures
Smol
A small and fast async runtime for Rust
Stars: ✭ 2,206 (+2106%)
Mutual labels:  concurrency, futures
Asyncio
asyncio historical repository
Stars: ✭ 952 (+852%)
Mutual labels:  concurrency, async-await
Ea Async
EA Async implements async-await methods in the JVM.
Stars: ✭ 1,085 (+985%)
Mutual labels:  concurrency, async-await
Concurrencpp
Modern concurrency for C++. Tasks, executors, timers and C++20 coroutines to rule them all
Stars: ✭ 340 (+240%)
Mutual labels:  concurrency, async-await
python3-concurrency
Python3爬虫系列的理论验证,首先研究I/O模型,分别用Python实现了blocking I/O、nonblocking I/O、I/O multiplexing各模型下的TCP服务端和客户端。然后,研究同步I/O操作(依序下载、多进程并发、多线程并发)和异步I/O(asyncio)之间的效率差别
Stars: ✭ 49 (-51%)
Mutual labels:  concurrency, futures
java-red
Effective Concurrency Modules for Java
Stars: ✭ 25 (-75%)
Mutual labels:  concurrency, futures
conquerant
lightweight async/await for Clojure
Stars: ✭ 31 (-69%)
Mutual labels:  concurrency, async-await
Shift
Light-weight EventKit wrapper.
Stars: ✭ 31 (-69%)
Mutual labels:  concurrency, async-await
async-enumerable-dotnet
Experimental operators for C# 8 IAsyncEnumerables
Stars: ✭ 32 (-68%)
Mutual labels:  concurrency, async-await
P Map
Map over promises concurrently
Stars: ✭ 639 (+539%)
Mutual labels:  concurrency, async-await
Funfix
Functional Programming Library for JavaScript, TypeScript and Flow ✨⚡️
Stars: ✭ 596 (+496%)
Mutual labels:  concurrency, futures
Brightfutures
Write great asynchronous code in Swift using futures and promises
Stars: ✭ 1,890 (+1790%)
Mutual labels:  concurrency, futures
Thread Pool
Thread pool implementation using c++11 threads
Stars: ✭ 417 (+317%)
Mutual labels:  concurrency, futures
futureproof
Bulletproof concurrent.futures
Stars: ✭ 36 (-64%)
Mutual labels:  concurrency, futures
Swoole Src
🚀 Coroutine-based concurrency library for PHP
Stars: ✭ 17,175 (+17075%)
Mutual labels:  concurrency, swoole
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+3679%)
Mutual labels:  concurrency, async-await
await-lock
Mutex locks for async functions
Stars: ✭ 66 (-34%)
Mutual labels:  concurrency, async-await

Swoole Futures

https://github.com/leocavalcante/swoole-futures/actions?query=workflow%3ACI https://shepherd.dev/github/leocavalcante/swoole-futures

Futures, Streams & Async/Await for PHP's Swoole asynchronous run-time.

Inspired by futures Crate for Rust's Tokio asynchronous run-time.

It's on top of Swoole's coroutines system there is no special wizardry, just sugar.

Install

composer require leocavalcante/swoole-futures

Usage

Async / await

Creates and awaits for asynchronous computations in an alternative style than Swoole's coroutines.

$future = Futures\async(fn() => 1);
$result = $future->await(); // 1

Futures are lazy, it only runs when you call await.

Join

Joins a list of Futures into a single Future that awaits for a list of results.

$slow_rand = function (): int {
    Co::sleep(3);
    return rand(1, 100);
};

$n1 = Futures\async($slow_rand);
$n2 = Futures\async($slow_rand);
$n3 = Futures\async($slow_rand);

$n = Futures\join([$n1, $n2, $n3]);

print_r($n->await());

This takes 3 seconds, not 9, Futures runs concurrently! (Order isn't guaranteed)

Race

Returns the result of the first finished Future.

use Swoole\Coroutine\Http\Client;

$site1 = Futures\async(function() {
    $client = new Client('www.google.com', 443, true);
    $client->get('/');
    return $client->body;
});

$site2 = Futures\async(function() {
    $client = new Client('www.swoole.co.uk', 443, true);
    $client->get('/');
    return $client->body;
});

$site3 = Futures\async(function() {
    $client = new Client('leocavalcante.dev', 443, true);
    $client->get('/');
    return $client->body;
});

$first_to_load = Futures\race([$site1, $site2, $site3]);

echo $first_to_load;

And there is a Futures\select alias.

Async map

Maps an array into a list of Futures where which item runs concurrently.

$list = [1, 2, 3];
$multiply = fn(int $a) => fn(int $b) => $a * $b;
$double = $multiply(2);

$doubles = Futures\join(Futures\async_map($list, $double))->await();

print_r($doubles);

Then

Sequences a series of steps for a Future, is the serial analog for join:

use function Futures\async;

$future = async(fn() => 2)
    ->then(fn(int $i) => async(fn() => $i + 3))
    ->then(fn(int $i) => async(fn() => $i * 4))
    ->then(fn(int $i) => async(fn() => $i - 5));

echo $future->await(); // 15

Stream

Streams values/events from sink to listen with between operations.

$stream = Futures\stream()
    ->map(fn($val) => $val + 1)
    ->filter(fn($val) => $val % 2 === 0)
    ->map(fn($val) => $val * 2)
    ->listen(fn($val) => print("$val\n")); // 4 8 12 16 20

foreach (range(0, 9) as $n) {
    $stream->sink($n);
}

MIT © 2020

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].