PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 122 This week: 1All time: 9,466 This week: 560Up
Version License PHP version Categories
asynctask-7 2.0.12BSD License7.1Tools, 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.

Picture of Dmitry Mamontov
  Performance   Level  
Name: Dmitry Mamontov <contact>
Classes: 16 packages by
Country: Russian Federation Russian Federation
Age: 33
All time rank: 78226 in Russian Federation Russian Federation
Week rank: 52 Up2 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 6x

Documentation

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

  Files folder image Files  /  src  
File Role Description
Files folder imageAsyncTask (5 files, 3 directories)

  Files folder image Files  /  src  /  AsyncTask  
File Role Description
Files folder imageAdapter (1 file)
Files folder imageException (3 files)
Files folder imageInterfaces (1 file)
  Plain text file Adapter.php Class Class source
  Plain text file AsyncTask.php Class Class source
  Plain text file Collection.php Class Class source
  Plain text file PidSystem.php Class Class source
  Plain text file Process.php Class Class source

  Files folder image Files  /  src  /  AsyncTask  /  Adapter  
File Role Description
  Plain text file SharedMemoryAdapter.php Class Class source

  Files folder image Files  /  src  /  AsyncTask  /  Exception  
File Role Description
  Plain text file AdapterException.php Class Class source
  Plain text file AsyncTaskException.php Class Class source
  Plain text file ProcessException.php Class Class source

  Files folder image Files  /  src  /  AsyncTask  /  Interfaces  
File Role Description
  Plain text file AdapterInterface.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageAsyncTask (2 directories)
  Accessible without login Plain text file bootstrap.php Example Example script

  Files folder image Files  /  tests  /  AsyncTask  
File Role Description
Files folder imageExample (2 files)
Files folder imageTests (6 files)

  Files folder image Files  /  tests  /  AsyncTask  /  Example  
File Role Description
  Plain text file ExampleAdapter.php Class Class source
  Plain text file ExampleTask.php Class Class source

  Files folder image Files  /  tests  /  AsyncTask  /  Tests  
File Role Description
  Plain text file AdapterTest.php Class Class source
  Plain text file AsyncTaskTest.php Class Class source
  Plain text file CollectionTest.php Class Class source
  Plain text file PidSystemTest.php Class Class source
  Plain text file ProcessTest.php Class Class source
  Plain text file SharedMemoryAdapterTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:122
This week:1
All time:9,466
This week:560Up