# joopschilder/php-async ## Introduction This package provides functions to run callables asynchronously in PHP. Return values are shared via System-V shared memory.
To use this package, you'll need PHP >= 7.0.0 with `ext-sysvshm` and `ext-pcntl`.
You should consider the state of this package to be experimental.

Note: This package should not be used in a CGI environment. The key that is used to access the block of shared memory is created based on the inode information of one of the source files. This means that, whenever multiple instances (processes) from the same project source are created, they will try to use the same block of memory and collisions will occur. I might swap the `ftok()` call for a random string generator somewhere down the road.

Note: It is possible (but discouraged) to change the amount of available shared memory. If you wish to do so, it's as simple as calling either `Runtime::_setSharedMemorySizeMB();` or `Runtime::_setSharedMemorySizeB();`.
If you want to use 32MB for example, call `Runtime::_setSharedMemorySizeMB(32);`.
Be sure to make this call before using any of the asynchronous functionalities. ## Installation This package is available on Packagist and can be installed using Composer:
```bash $ composer require joopschilder/async-php ``` It's also possible to manually add it to your `composer.json`: ```json { "require": { "joopschilder/php-async": "dev-master" } } ``` ## Usage #### Functions The library exposes three functions in the global namespace that provide indirect access to the class `Asynchronous`: * `async(callable $function, ...$parameters)` to run something asynchronously, giving back a `Promise`; * `async_wait_all()` to wait for all currently running jobs to finish; * `async_reap_zombies()` to clean up any zombie processes during runtime if any exist; #### Promises Whenever you call `async(...)`, a `Promise` instance is returned.
A `Promise` is considered to be resolved when the function it belongs to returned a value or finished execution. To block execution until a promise is resolved, simply call the `resolve()` method on the promise. It's possible to check whether the promise has been resolved in a non-blocking way by calling the `isResolved()` method.
You can actually return anything that is serializable in PHP: objects, arrays, strings, you name it. ```php resolve(); $pid = $promise->getValue(); ``` The shutdown handler and destructors should take care of the cleanup.
#### Asynchronous curl requests ... though you should probably look into curl multi handles for this: curl_multi_init(). ```php To track what's happening in real time, I like to use:
```bash $ watch -n 1 "ipcs -m --human && ipcs -m -p && ipcs -m -t && ipcs -m -u" ```
To clean all 'unused' shared memory blocks (they might remain resident in RAM if your program terminated unexpectedly):
```bash $ ipcrm -a ``` ## What's next? - Improving stability - Add an explaining diagram