PHP Classes

async: Run code asynchronously using the PHP program

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 197 All time: 8,512 This week: 78Up
Version License PHP version Categories
async 1.0MIT/X Consortium ...5PHP 5, Language
Description 

Author

This class can run code asynchronously using the PHP program.

It can take the code of a given closure and makes it run using the PHP program to create a separate process.

The class can take callback functions to run when the called code finishes, or in case of error.

Picture of Kacper Rowinski
  Performance   Level  
Name: Kacper Rowinski <contact>
Classes: 14 packages by
Country: Poland Poland
Age: 40
All time rank: 91426 in Poland Poland
Week rank: 192 Up2 in Poland Poland Up
Innovation award
Innovation award
Nominee: 8x

Example

<?php


namespace bin;


include
__DIR__ . '/../vendor/autoload.php';

use
Async\AsyncCall;

$s = microtime(true);


AsyncCall::run(
    function () {
       
sleep(2);
       
// there is no callback so echo will not be showed
       
echo 'sleep 2s' . PHP_EOL;
    }
);

AsyncCall::run(
    function () {
       
sleep(1);
       
// echo will be catched in child process and returned as echo in parent process ( but empty callback MUST be set )
       
echo 'sleep 1s' . PHP_EOL;
    },
    function () { }
);

AsyncCall::run(
    function () {
        throw new \
Exception('bar');
    }
);

AsyncCall::run(
    function () {
        throw new \
Exception('foo');
    },
    function () {},
    function (\
Exception $error) { assert($error->getMessage() === 'foo'); }
);


AsyncCall::run(
    function () {
       
// if this is in parent, child will not see this
       
function getPage($url)
        {
           
$ch = curl_init();
           
curl_setopt($ch, CURLOPT_URL, $url);
           
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
           
$output = curl_exec($ch);
           
curl_close($ch);

            return
$output;
        }

        return
getPage('example.com');
    },
    function (
$results) {
        echo
$results;
    }
);


echo
PHP_EOL;
echo
microtime(true) - $s;
echo
PHP_EOL;


Details

How this work?

Well its basically "exec" with serialised closure. "Dressed" in nice libs like symfony process and console. I serialise callable function and sent to child process by exec. To get callback I register shutdown function and wait for process to finish.

Why not pcntl ?

  • Pcntl extension fork, so you can forget using it in web applications like apache2/php-fpm etc its only for CLI
  • forks retains the parent state (for example open files) so its problematic

Problems ?

  • Calling exec is slower then fork
  • Some resource/function/data must be passed directly to closure

Some research

  • https://www.phproundtable.com/episode/asynchronous-php - good start to "know how" make php async
  • https://amphp.org/ - non-blocking framework for PHP

Example ?

Sure take a look - https://github.com/krowinski/async/blob/master/example/example.php

Supports M$ Windows?

NO.

TODO

  • process limit
  • timeouts
  • tests

  Files folder image Files (7)  
File Role Description
Files folder imagebin (1 file)
Files folder imageexample (1 file)
Files folder imagesrc (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (7)  /  bin  
File Role Description
  Accessible without login Plain text file console Example Example script

  Files folder image Files (7)  /  example  
File Role Description
  Accessible without login Plain text file example.php Example Example script

  Files folder image Files (7)  /  src  
File Role Description
Files folder imageAsync (3 files)

  Files folder image Files (7)  /  src  /  Async  
File Role Description
  Plain text file AsyncCall.php Class Class source
  Plain text file AsyncChildCommand.php Class Class source
  Plain text file AsyncChildResponse.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:197
This week:0
All time:8,512
This week:78Up