运行 部分 PHP 代码从 PHP 程序内部异步
Run part of PHP code asynchronously from inside PHP program
我有一些 PHP 脚本,我从外部 class 调用方法。我想异步 运行 这个方法。我不想阻止程序的其余部分。此方法在后台执行一些操作,return 什么都不做,因此无需等待它完成。在 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');
如果这有帮助,我正在使用 Fat Free Framework 和 PHP 5.6 非线程安全
使用线程。
class PostDataHandlerAsync extends Thread
{
private $postData
public function __construct($postData)
{
$this->postData = $postData;
}
public function run()
{
/*
Your code goes here
*/
}
}
$postData = $this->f3->get('POST');
$obj = new PostDataHandlerAsync($postData);
$obj->run();
您可以使用 $f3->abort()
将 output/response 发送到浏览器,然后再处理您的其他阻止功能。这不是真正的异步解决方案,但可以工作。您也可以使用 php-icicle 之类的东西来添加线程支持,但这可能需要安装其他一些 php 模块。
我有一些 PHP 脚本,我从外部 class 调用方法。我想异步 运行 这个方法。我不想阻止程序的其余部分。此方法在后台执行一些操作,return 什么都不做,因此无需等待它完成。在 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');
如果这有帮助,我正在使用 Fat Free Framework 和 PHP 5.6 非线程安全
使用线程。
class PostDataHandlerAsync extends Thread
{
private $postData
public function __construct($postData)
{
$this->postData = $postData;
}
public function run()
{
/*
Your code goes here
*/
}
}
$postData = $this->f3->get('POST');
$obj = new PostDataHandlerAsync($postData);
$obj->run();
您可以使用 $f3->abort()
将 output/response 发送到浏览器,然后再处理您的其他阻止功能。这不是真正的异步解决方案,但可以工作。您也可以使用 php-icicle 之类的东西来添加线程支持,但这可能需要安装其他一些 php 模块。