Search code examples
phpasynchronousfat-free-framework

Run part of PHP code asynchronously from inside PHP program


I have some PHP script where I invoke method from the external class. I want to run this method asynchronously. I don't want to block rest of the program. This method does some work in the background and return nothing so there is no need to wait while it finished. Is there a way to do this in PHP?

# get post data from user
$postData = $this->f3->get('POST');

# start of asynchronous part
$obj = new asyncClass();
$obj->init($postData);
# end of asynchronous part

# do some work with post data
$soc = new someOtherClass($postData);
$result = $soc->send();
# assign result of work to variable
$this->f3->set('var', $result);
# show it to user
$this->f3->set('view', 'view.html');

If this can help, I'm using Fat Free Framework and PHP 5.6 Non-Thread Safe


Solution

  • 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.