PHP Classes
elePHPant
Icontem

PHP Asynchronous Task 7: Execute parallel task using a sub-class

Recommend this page to a friend!
  Info   View files Documentation   View files View files (25)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-02-07 (2 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: 53 This week: 53All time: 9,388 This week: 6Up
Version License PHP version Categories
asynctask-7 2.0.12BSD License7.1Utilities and Tools, Language, PHP 7
Description Author

This package can execute parallel task using a sub-class.

It provides a base class that can be called to start a task that would executed in parallel.

A sub-class may extend the base class to define functions for customizing the execution of the parallel task, like before the task is started, what the task will do in parallel, and output the progress of the parallel task.

  Performance   Level  
Innovation award
Innovation award
Nominee: 5x

Details

Build Status Latest Stable Version License Total Downloads PHP Classes

AsyncTask

AsyncTask enables proper and easy use of the thread. This class allows to perform background operations and publish results on the thread without having to manipulate threads and/or handlers.

The early class implementation supports PHP 5.3, but does not support what is implemented in this class.

Requirements

  • PHP version ~7.1.0
  • Module installed pcntl and posix
  • All functions pcntl, posix removed from the directive disable_functions
  • SharedMemoryAdapter: - All functions shm removed from the directive disable_functions

Installation

1) Install composer

2) Follow in the project folder:

composer require dmamontov/asynctask-7 ~2.0.13

In config composer.json your project will be added to the library dmamontov/asynctask-7, who settled in the folder vendor/. In the absence of a config file or folder with vendors they will be created.

If before your project is not used composer, connect the startup file vendors. To do this, enter the code in the project:

require 'path/to/vendor/autoload.php';

Adapter list

  • SharedMemory - working
  • ~~FileSystem~~ - in the process
  • ~~Redis~~ - in the process

Offer adapters that are missing. We develop!

Examples

Example of work

use AsyncTask\{
    AsyncTask,
    Collection
};

class TestTask extends AsyncTask
{
    protected function onPreExecute(Collection $collection)
    {
    }

    protected function doInBackground(Collection $collection)
    {
        return 'My First Task';
    }

    protected function onPostExecute($result)
    {
        echo $result;
    }

    protected function publishProgress()
    {
        echo rand(0,9) . PHP_EOL;
    }
}

$task = new TestTask();
$task
    ->setTitle('TestTask')
    ->execute(new Collection);

Task Example

use AsyncTask\AsyncTask;
use AsyncTask\Collection;

class ExampleTask extends AsyncTask
{
    /
     * Optional method.
     */
    protected function onPreExecute(Collection $collection)
    {
        return $collection;
    }

    /
     * Required method.
     */
    protected function doInBackground(Collection $collection)
    {
        return $collection;
    }

    /
     * Optional method.
     * With this method, an additional process is created.
     */
    protected function publishProgress()
    {
    }

    /
     * Optional method.
     */
    protected function onPostExecute($result)
    {
    }

    /
     * Optional method.
     */
    protected function onCancelled()
    {
    }
}

Adapter Example

use AsyncTask\Adapter;
use AsyncTask\Interfaces\AdapterInterface;

class ExampleAdapter extends Adapter implements AdapterInterface
{

    /
     * Required method.
     */
    public function init(): AdapterInterface
    {
        return $this;
    }
    
    /
     * Required method.
     */
    public function finish(): AdapterInterface
    {
        return $this;
    }

    /
     * Required method.
     */
    public function clean(bool $parent = false): AdapterInterface
    {
        return $this;
    }
    
    /
     * Required method.
     */
    public function has($key, bool $parent = false): bool
    {
        return false;
    }

    /
     * Required method.
     */
    public function remove($key, bool $parent = false): bool
    {
        return true;
    }
    
    /
     * Required method.
     */
    public function get($key, bool $parent = false)
    {
        return null;
    }

    /
     * Required method.
     */
    public function write($key, $val, bool $parent = false): AdapterInterface
    {
        return $this;
    }
}

ToDo

  • More tests.
  • More adapters.
  • Class for managing running processes.
  Files folder image Files  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagetests (1 file, 1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file phpunit.xsd Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:53
This week:53
All time:9,388
This week:6Up