PHP Threading Call to a PHP Function Asynchronously

PHP threading call to a php function asynchronously

http://php.net/pthreads

http://docs.php.net/Thread

PHP certainly can support threading. Loading data from a SQL/NoSQL database in parallel is definitely a possibility. See the PHP manual, examples found in github and pecl packages, and a little bit more info on http://pthreads.org

Please note, the documentation did state that this is part of the core, this is ( my ) human error. You must install pthreads with a thread-safe version of php, it can be installed from pecl ( windows incuded, dlls @ http://windows.php.net/downloads/pecl/releases/pthreads ). Apologies.

How to use php function asynchronously

You can't, php is not meant to work that way.

You can run a separate background process, which checks database for jobs to be done and does them. But that is not true async. It's just a background worker.

Asynchronous Method Call In PHP

You cannot execute a method asynchronously. But you could return the data to the client, close the connection, and execute your time consuming method once you disconnected.

This answer goes in details.

Another solution to execute php code asynchronously is forking a new process with pclose(popen()).

Or for a really advanced solution you could look into the threading module of PHP.

Running async function in php

Recent versions of pthreads support closures as members, making the code very simple:

<?php
class Background extends Thread {

public function __construct(callable $call, array $args = []) {
$this->call = $call;
$this->args = $args;
}

public function run() {
call_user_func_array($this->call, $this->args);
}

protected $call;
protected $args;
}

$background = new Background(function($greeting){
printf("%s\n", $greeting);
}, ["Hello World"]);
$background->start();
$background->join();

function named($greeting) {
printf("%s\n", $greeting);
}

$background = new Background("named", ["Goodbye World"]);
$background->start();
$background->join();
?>

However, this is horrible, it's hard to imagine any function that is so hungry that it requires a thread of it's own.

You have started down the right path with the thought that you should reuse the context and create a worker thread, pthreads has all of this built in.

More sensible code using built in classes looks more like:

<?php
class Background extends Threaded {

public function __construct(callable $call, array $args = []) {
$this->call = $call;
$this->args = $args;
}

public function run() {
call_user_func_array($this->call, $this->args);
}

protected $call;
protected $args;
}

$pool = new Pool(4);

$pool->submit(new Background(function($greeting){
printf("%s\n", $greeting);
}, ["Hello World"]));

$pool->shutdown();
?>

But this still doesn't deal with a return value. I'll assume that you want to retrieve the result of calls made using the Pool, in that case the code looks more like:

<?php
class Background extends Threaded {

public function __construct(callable $call, array $args = []) {
$this->call = $call;
$this->args = $args;
}

public function run() {
$this->synchronized(function(){
$this->result = call_user_func_array
($this->call, $this->args);
$this->notify();
});
}

public function getResult() {
return $this->synchronized(function(){
while (!isset($this->result))
$this->wait();
return $this->result;
});
}

protected $call;
protected $args;
protected $result;
}

$pool = new Pool(4);

$call = new Background(function($greeting){
return sprintf("%s\n", $greeting);
}, ["Hello World"]);

$pool->submit($call);

echo $call->getResult();

$pool->shutdown();
?>

As you can see, a call to Background::getResult will result in the calling context waiting until a result is available, this may or may not be desirable, but makes for a good example.

Run asynchronous while functions or multithread

You can read about it(A succinct parallel concurrency API for PHP 7)-> https://github.com/krakjoe/parallel

How can do php asynchronous? It is possible for using pthreads

You should start an asynchronous command line php script.

using exec() function.

Example

exec(“php asyn.php”.” > /dev/null 2>/dev/null &“);

Run part of PHP code asynchronously from inside PHP program

You can use $f3->abort() to send the output/response to the browser and process your other blocking function afterwards. That's not a real asynchron solution but would work. You could also use something like php-icicle to add threads support, but that maybe requires some other php modules being installed.



Related Topics



Leave a reply



Submit