Download the PHP package oliverde8/asynchronous-jobs without Composer
On this page you can find all versions of the php package oliverde8/asynchronous-jobs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download oliverde8/asynchronous-jobs
More information about oliverde8/asynchronous-jobs
Files in oliverde8/asynchronous-jobs
Package asynchronous-jobs
Short Description Execute new php instances easily as you would threads.
License MIT
Informations about the package asynchronous-jobs
Asynchronous - Jobs
PHP AsynchronousJobs is a small library allowing to run PHP code in parallel of your main code. In some ways it works like threads but it doesen't use the proper system libraries to do so. It is a workaround
The library was created to work on windows & linux systems without the need of installing php pthreads extension. The library must not be considered as replacement to threads !! The way the library works isn't optimized to for that.
It is built in the purpose of doing long tasks in parallel. A good example would be to download filess while the main script is serving informations to users. The library is ment to be used for command line tools & deamons.
Why
Even throught pthreads is fantastic many servers don't have it installed, and therefore adding a dependency to that can be blocking. In this case the library was built for the ml-expansion project which is used by people that wishes to have something that just works. This library answers to that question.
If you need to run a few threads time to time without wanting to add a dependency to pthreads this library might just be want you need.
FAQ
https://github.com/oliverde8/PHP-AsynchronousJobs/wiki/Faq
Usage
First you need to create a job :
Then you can create a job :
Let's create a second job as well.
Now execute the jobs
You should see first "I end after 2" then the message "I end after 3"
Waiting for ongoing jobs
Once you started some jobs you may decide you need to wait for them to finish.
So once you have started your jobs :
You can use wait all :
This will block you instance until all jobs habe finished their execution. waitForAll takes in parameters the sleep time. So if you know your jobs takes a few jours to run you can increase the sleep time to a few minutes to prevent IO to be over used.
You may also wish for just one job to finish :
The process will be blocked until the job1 is finished.
Making a Curl request.
This is a very simple implementation for doing curl queries. It should be improved ! But well it can be built open the job executions.
You can of crouse pass some parameters and do a POST queries as well.
Having callbacks
Something else you can do is place callbacks on your jobs in order to have a function called when the job is done.
Custom settings
Yo have custom settings you must call
Before running initiating any jobs before, it needs to be your first call !!
The methods can take the fallowing parameters :
- $id Allows you to share instances between multiple process, leave it null if you don't understand it should rarely need changing.
- $phpExecutable Path to the php executable, can be an issue on windows, on linux php alone should suffice.
- $tmpPath Path to put the temporary files used to synchronize the process. It needs to be writable by the process.
Stuff to now
Ignored variables :
All variables you set in a job is transfered to the new execution(thread) once the job starts and are then updated when to job finishes with the new values coming from the job. This varibles may be privete or public, they are going to be serialized and passed to the new execution. Once the execution finishes they will be serialized again and sent back to the main execution.
In some cases we may wish to have values available only on the main process or on the new execution. All variables prefixed by a double underscore("_") will be ignored during transfer.
Example :
TODO In the future
- Redis support (so much cooler & faster)
- Note for my self : I need to separate the current JobRunner's content so that the data management section can be separated from the logic.
- Semaphore support
- Shared memory between jobs & instance
- The jobs will do when they wish to share some new data, and on the main process we will call on the job to get the latest information. The purpose for this isn't to sync a lot of data but just keep track of progress or something.