PHP中的异步方法调用

时间:2015-02-10 13:11:16

标签: php

我有以下困境,我有一个SOAP API,需要优化一个方法:

public function myMethodForOptimisation(array $options) {
    //doing some stuff

    $this->requestsAnUltraSlowTimeConsumingMethod();

    return $someData;
}

我真的不需要等待$this->requestsAnUltraSlowTimeConsumingMethod() i的最终确定,我只是异步地想对这种方法做些什么,并将结果return告诉客户。

需要将其转换为:

public function myMethodForOptimisation(array $options) {
    //doing some stuff

    async_method_call($this->requestsAnUltraSlowTimeConsumingMethod());

    return $someData;
}

PHP中是否有async_method_call()?

2 个答案:

答案 0 :(得分:3)

您无法异步执行方法。但是,您可以将数据返回给客户端,关闭连接,并在断开连接后执行耗时的方法。

This answer goes in details.

异步执行php代码的另一个解决方案是使用pclose(popen())分配新进程。

或者对于一个非常先进的解决方案,您可以查看PHP的线程模块。

答案 1 :(得分:1)

您可以使用主题:PHP threading call to a php function asynchronously

这是唯一的方法。有几种方法可以使用它们,所以选择最好/最简单的方法。

我正常使用这个:http://php.net/manual/en/class.thread.php

相关问题