Download the PHP package appserver-io/concurrency without Composer
On this page you can find all versions of the php package appserver-io/concurrency. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download appserver-io/concurrency
More information about appserver-io/concurrency
Files in appserver-io/concurrency
Package concurrency
Short Description It introduces abstract services and objects that provides easy handling for thread-safety, concurrency and sharing.
License OSL-3.0
Homepage https://github.com/appserver-io/concurrency
Informations about the package concurrency
Toolkit for Concurrent Programming in PHP with pthreads
What is it?
The concurrency toolkit introduces abstract services and objects that provides easy handling for thread-safety, concurrency programming and data sharing all written in PHP-Userland.
Why?
If you are implementing multithreaded functionality you're always fighting against the same problems called Race-Conditions, Deadlocks etc... When sharing simple data or even plain PHP objects between threads you have to make sure, that everything is synchronized (Thread-safe) in any logic of your code. This Toolkit mostly helps you to avoid those problems by providing abstract services and object written in PHP-Userland.
How to use?
This toolkit is in early-stage development. Please use with caution.
The ExecutorService
It allows you to hold plain PHP objects persistent as singleton in memory accessible everywhere in your code even in different thread contexts. You can keep those plain PHP objects easily thread-safe or call certain methods asynchronous by using simple annotations like @Synchronized
or @Asynchronous
in method docblocks.
How it works
If you want to use the concurrency toolkit in your project you have to add it to the composer dependencies composer.json
and do a composer update
afterwards.
At first you have to initialise the ExecutorService
in a main PHP script main.php
.
Lets say you want to build a simple storage PHP object to share data all over your multithreaded implementations. The storage object could look like this. Create Storage.php
and add:
Maybe you've noticed the method docblock annotations we used here. Methods marked with @Synchronized
can not be called more than once at the same time. That means the $data
array will always be synchronized when using all()
, set()
, get()
, del()
or inc()
. Methods using @Asynchronous
are called asynchronously. So if the dump()
method is called it will dump the $data
array while not blocking your main logic execution.
To make use of the storage object we have to create a multithreaded task simulation that should represent multithreaded business logic. So create Task.php
and add:
Now bring all together and enhance the main script main.php
:
If you call main.php
with a thread-safe compiled php version where pthreads ext is installed as
it`s provided by appserver.io runtime for example the result should look like this:
The executor service has some internal function as described here:
Method | Description |
---|---|
__return |
Returns the plain entity object from executor thread context if its serializable in its actual state |
__invoke(closure) |
Executes the callable in executor thread context. It will provide $self as function argument which references the plain entity object. Example usage: $executorServiceEntity->__invoke(function($self) { $self->doSomething() }); |
__reset() |
Resets the plaing entity object in executor service context |
__shutdown() |
Shutdown the executor service thread |
Issues
In order to bundle our efforts we would like to collect all issues regarding this package in the main project repository's issue tracker.
Please reference the originating repository as the first element of the issue title e.g.:
[appserver-io/<ORIGINATING_REPO>] A issue I am having