• Stars
    star
    161
  • Rank 233,470 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Non-blocking synchronization primitives for PHP based on Amp and Revolt.

amphp/sync

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. amphp/sync specifically provides synchronization primitives such as locks and semaphores for asynchronous and concurrent programming.

Latest Release MIT License

Installation

This package can be installed as a Composer dependency.

composer require amphp/sync

Usage

The weak link when managing concurrency is humans; so amphp/sync provides abstractions to hide some complexity.

Mutex

Mutual exclusion can be achieved using Amp\Sync\synchronized() and any Mutex implementation, or by manually using the Mutex instance to acquire a Lock.

As long as the resulting Lock object isn't released using Lock::release() or by being garbage collected, the holder of the lock can exclusively run some code as long as all other parties running the same code also acquire a lock before doing so.

function writeExclusively(Amp\Sync\Mutex $mutex, string $filePath, string $data) {
    $lock = $mutex->acquire();
    
    try {
        Amp\File\write($filePath, $data);
    } finally {
        $lock->release();
    }
}
function writeExclusively(Amp\Sync\Mutex $mutex, string $filePath, string $data) {
    Amp\Sync\synchronized($mutex, fn () => Amp\File\write($filePath, $data));
}

Semaphore

Semaphores are another synchronization primitive in addition to mutual exclusion.

Instead of providing exclusive access to a single party, they provide access to a limited set of N parties at the same time. This makes them great to control concurrency, e.g. limiting an HTTP client to X concurrent requests, so the HTTP server doesn't get overwhelmed.

Similar to Mutex, Lock instances can be acquired using Semaphore::acquire(). Please refer to the Mutex documentation for additional usage documentation, as they're basically equivalent except for the fact that Mutex is always a Semaphore with a count of exactly one party.

In many cases you can use amphp/pipeline instead of directly using a Semaphore.

Parcel

A Parcel is used to synchronize access to a value across multiple execution contexts, such as multiple coroutines or multiple processes. The example below demonstrates using a LocalParcel to share an integer between two coroutines.

use Amp\Future;
use Amp\Sync\LocalMutex;
use Amp\Sync\LocalParcel;
use function Amp\async;
use function Amp\delay;

$parcel = new LocalParcel(new LocalMutex(), 42);

$future1 = async(function () use ($parcel): void {
    echo "Coroutine 1 started\n";

    $value = $parcel->synchronized(function (int $value): int {
        delay(1); // Delay for 1s to simulate I/O.
        return $value * 2;
    });

    echo "Value after access in coroutine 1: ", $value, "\n";
});

$future2 = async(function () use ($parcel): void {
    echo "Coroutine 2 started\n";

    $value = $parcel->synchronized(function (int $value): int {
        delay(1); // Delay again in this coroutine.
        return $value + 8;
    });

    echo "Value after access in coroutine 2: ", $value, "\n";
});

Future\await([$future1, $future2]);

Channels

Channels are used to send data between execution contexts, such as multiple coroutines or multiple processes. The example below shares two Channel between two coroutines. These channels are connected. Data sent on a channel is received on the paired channel and vice-versa.

use Amp\Future;
use function Amp\async;
use function Amp\delay;

[$left, $right] = createChannelPair();

$future1 = async(function () use ($left): void {
    echo "Coroutine 1 started\n";
    delay(1); // Delay to simulate I/O.
    $left->send(42);
    $value = $left->receive();
    echo "Received ", $value, " in coroutine 1\n";
});

$future2 = async(function () use ($right): void {
    echo "Coroutine 2 started\n";
    $value = $right->receive();
    echo "Received ", $value, " in coroutine 2\n";
    delay(1); // Delay to simulate I/O.
    $right->send($value * 2);
});

Future\await([$future1, $future2]);

Sharing data between processes

To share data between processes in PHP, the data must be serializable and use external storage or an IPC (inter-process communication) channel.

Parcels in external storage

SharedMemoryParcel uses shared memory conjunction with PosixSemaphore wrapped in SemaphoreMutex (though another cross-context mutex implementation may be used, such as RedisMutex in amphp/redis).

Note
ext-shmop and ext-sysvmsg are required for SharedMemoryParcel and PosixSemaphore respectively.

amphp/redis provides RedisParcel for storing shared data in Redis.

Channels over pipes

Channels between processes can be created by layering serialization (Native PHP serialization, JSON serialization, etc.) on a pipe between those processes.

StreamChannel in amphp/byte-stream creates a channel from any ReadableStream and WritableStream. This allows a channel to be created from a variety of stream sources, such as sockets or process pipes.

ProcessContext and task Execution objects in amphp/parallel provide a Channel to send data between processes and tasks.

Concurrency Approaches

Given you have a list of URLs you want to crawl, let's discuss a few possible approaches. For simplicity, we will assume a fetch function already exists, which takes a URL and returns the HTTP status code (which is everything we want to know for these examples).

Approach 1: Sequential

Simple loop using non-blocking I/O, but no concurrency while fetching the individual URLs; starts the second request as soon as the first completed.

$urls = [...];

$results = [];

foreach ($urls as $url) {
    $results[$url] = fetch($url);
}

var_dump($results);

Approach 2: Everything Concurrently

Almost the same loop, but awaiting all operations at once; starts all requests immediately. Might not be feasible with too many URLs.

$urls = [...];

$results = [];

foreach ($urls as $url) {
    $results[$url] = Amp\async(fetch(...), $url);
}

$results = Amp\Future\await($results);

var_dump($results);

Approach 3: Concurrent Chunks

Splitting the jobs into chunks of ten; all requests within a chunk are made concurrently, but each chunk sequentially, so the timing for each chunk depends on the slowest response; starts the eleventh request as soon as the first ten requests completed.

$urls = [...];

$results = [];

foreach (\array_chunk($urls, 10) as $chunk) {
    $futures = [];

    foreach ($chunk as $url) {
        $futures[$url] = Amp\async(fetch(...), $url);
    }

    $results = \array_merge($results, Amp\Future\await($futures));
}

var_dump($results);

Approach 4: ConcurrentIterator

The amphp/pipeline library provides concurrent iterators which can be used to process and consume data concurrently in multiple fibers.

use Amp\Pipeline\Pipeline;
use function Amp\delay;

$urls = [...];

$results = Pipeline::fromIterable($urls)
    ->concurrent(10) // Process up to 10 URLs concurrently
    ->unordered() // Results may arrive out of order
    ->map(fetch(...)) // Map each URL to fetch(...)
    ->toArray();

var_dump($results);

See the documentation in amphp/pipeline for more information on using Pipelines for concurrency.

Versioning

amphp/sync follows the semver semantic versioning specification like all other amphp packages.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.

More Repositories

1

amp

A non-blocking concurrency framework for PHP applications. 🐘
PHP
4,239
star
2

http-server

An advanced async HTTP server library for PHP, perfect for real-time apps and APIs with high concurrency demands.
PHP
1,287
star
3

parallel

An advanced parallelization library for PHP, enabling efficient multitasking, optimizing resource use, and application responsiveness through multiple CPU threads.
PHP
783
star
4

http-client

An advanced async HTTP client library for PHP, enabling efficient, non-blocking, and concurrent requests and responses.
PHP
701
star
5

byte-stream

A non-blocking stream abstraction for PHP based on Amp.
PHP
367
star
6

mysql

An async MySQL client for PHP, optimizing database interactions with efficient non-blocking capabilities. Perfect for responsive, high-performance applications.
PHP
358
star
7

thread

Unmaintained. Use https://github.com/amphp/parallel.
PHP
298
star
8

parallel-functions

Simplified parallel processing for PHP based on Amp.
PHP
271
star
9

ext-fiber

PHP Fiber extension
Assembly
239
star
10

process

An async process dispatcher for Amp.
PHP
229
star
11

socket

Non-blocking socket and TLS functionality for PHP based on Amp.
PHP
229
star
12

ext-uv

C
190
star
13

dns

Async DNS resolution for PHP based on Amp.
PHP
157
star
14

redis

Efficient asynchronous communication with Redis servers, enabling scalable and responsive data storage and retrieval.
PHP
156
star
15

websocket-client

Async WebSocket client for PHP based on Amp.
PHP
144
star
16

parser

A generator parser to make streaming parsers simple.
PHP
124
star
17

websocket-server

WebSocket component for PHP based on the Amp HTTP server.
PHP
114
star
18

serialization

Serialization tools for IPC and data storage in PHP.
PHP
110
star
19

cache

A fiber-aware cache API based on Amp and Revolt.
PHP
99
star
20

file

An abstraction layer and non-blocking file access solution that keeps your application responsive.
PHP
97
star
21

windows-registry

Windows Registry Reader.
PHP
97
star
22

postgres

Async Postgres client for PHP based on Amp.
PHP
96
star
23

hpack

HPack - HTTP/2 header compression implementation in PHP.
PHP
94
star
24

http

HTTP primitives which can be shared by servers and clients.
PHP
88
star
25

beanstalk

Asynchronous Beanstalk Client for PHP.
PHP
65
star
26

cluster

Building multi-core network applications with PHP.
PHP
60
star
27

aerys

A non-blocking HTTP application, WebSocket and file server for PHP based on Amp.
PHP
53
star
28

pipeline

Concurrent iterators and pipeline operations.
PHP
46
star
29

http-server-router

A router for Amp's HTTP Server.
PHP
38
star
30

getting-started

A getting started guide for Amp.
PHP
37
star
31

websocket

Shared code for websocket servers and clients.
PHP
36
star
32

green-thread

PHP
36
star
33

ssh

Async SSH client for PHP based on Amp.
PHP
35
star
34

log

Non-blocking logging for PHP based on Amp and Monolog.
PHP
33
star
35

injector

A recursive dependency injector used to bootstrap and wire together S.O.L.I.D., object-oriented PHP applications.
PHP
31
star
36

uri

Uri Parser and Resolver.
PHP
24
star
37

amphp.github.io

Main website repository.
HTML
24
star
38

react-adapter

Makes any ReactPHP library compatible with Amp.
PHP
24
star
39

artax

An async HTTP/1.1 client for PHP based on Amp.
PHP
23
star
40

http-server-static-content

An HTTP server plugin to serve static files like HTML, CSS, JavaScript, and images effortlessly.
PHP
22
star
41

phpunit-util

Helper package to ease testing with PHPUnit.
PHP
21
star
42

http-server-session

An HTTP server plugin that simplifies session management for your applications. Effortlessly handle user sessions, securely managing data across requests.
PHP
19
star
43

http-server-form-parser

An HTTP server plugin that simplifies form data handling. Effortlessly parse incoming form submissions and extracting its data.
HTML
18
star
44

aerys-reverse

Reverse HTTP proxy handler for Aerys
PHP
16
star
45

mysql-dbal

PHP
16
star
46

sql

Common interfaces for Amp based SQL drivers.
PHP
15
star
47

stomp

A non-blocking STOMP client built on the amp concurrency framework
PHP
15
star
48

loop

Discontinued. Merged into https://github.com/amphp/amp.
PHP
13
star
49

http-tunnel

This package provides an HTTP CONNECT tunnel for PHP based on Amp.
PHP
11
star
50

http-client-psr7

PSR-7 adapter for amphp/http-client.
PHP
10
star
51

http-client-cookies

Automatic cookie handling for Amp's HTTP client.
PHP
10
star
52

rpc

Remote procedure calls for PHP based on Amp.
PHP
9
star
53

http-client-cache

An async HTTP cache for Amp's HTTP client.
PHP
8
star
54

sql-common

Implementations shared by amphp/postgres and amphp/mysql
PHP
7
star
55

php-cs-fixer-config

Common code style configuration for all @amphp projects.
PHP
7
star
56

react-stream-adapter

Adapters to make React's and Amp's streams compatible.
PHP
7
star
57

http-client-guzzle-adapter

PHP
6
star
58

windows-process-wrapper

Child process wrapper to support non-blocking process pipes on Windows.
C
6
star
59

amphp.org

Documentation for AMPHP v3 based libraries.
HTML
6
star
60

quic

PHP
5
star
61

logo

Repository to store the logo and other assets.
3
star
62

dbus

A non-blocking DBus Connector with message serialization based on Amp.
PHP
2
star
63

website-tools

Website administration tools for amphp.org.
PHP
1
star
64

template

This repository serves as template for new amphp projects.
1
star
65

website-shared

Unmaintained. Has been merged into https://github.com/amphp/amphp.github.io.
1
star
66

.github

1
star