https://github.com/diloabininyeri/async
A library that can run objects async with PHP
https://github.com/diloabininyeri/async
async concurency mutex mutex-lock mutex-semaphore paralelism php
Last synced: about 2 months ago
JSON representation
A library that can run objects async with PHP
- Host: GitHub
- URL: https://github.com/diloabininyeri/async
- Owner: diloabininyeri
- Created: 2024-01-13T08:13:01.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-13T10:47:55.000Z (over 1 year ago)
- Last Synced: 2025-03-26T16:11:25.909Z (2 months ago)
- Topics: async, concurency, mutex, mutex-lock, mutex-semaphore, paralelism, php
- Language: PHP
- Homepage:
- Size: 7.81 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.MD
Awesome Lists containing this project
README
## Php async
Provides async running objects with PHP via process spawnfor installation via composer
```console
composer require zeus/async:dev-main```
php test code
```php
use Zeus\Async\AsyncProcess;
use Zeus\Async\test\Sleep;require_once 'vendor/autoload.php';
$asyncProcess = new AsyncProcess();
$asyncProcess->add(new Sleep(2));
$asyncProcess->add(new Sleep(1));
$asyncProcess->add(new Sleep(2));
$asyncProcess->add(new Sleep(1));
$asyncProcess->add(new Sleep(3));
$asyncProcess->add(new Sleep(1));
$asyncProcess->add(new Sleep(2));
$asyncProcess->add(new Sleep(2));
$asyncProcess->add(new Sleep(1));echo 'app started';
$asyncProcess->start();
echo 'app progressing';
$asyncProcess->wait();echo 'app is finished';
```
## Synchronous
If you want some objects to run in Sync, you can do this through the Mutex object.```php
use Zeus\Async\AsyncProcess;
use Zeus\Async\Mutex;
use Zeus\Async\test\Sleep;
use Zeus\Async\test\SleepSync;require_once 'vendor/autoload.php';
$mutex = new Mutex();
$asyncProcess = new AsyncProcess(7);
$asyncProcess->add(new SleepSync($mutex, 2));
$asyncProcess->add(new Sleep(1));
$asyncProcess->add(new SleepSync($mutex, 2));$asyncProcess->start();
$asyncProcess->wait();
```## Mutex
In critical transactions, some operations may need to wait for some operations, the best example of this is if two people try to buy a ticket at the same time, it will make one of them wait atomically.
```php$mutex = new Mutex();
$mutex->lock();
//critic operations
$mutex->unlock();
```