dongqing4774
2015-02-10 13:11
浏览 85
已采纳

PHP中的异步方法调用

I have the following dilemma, I have a SOAP API, need to optimise a method:

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

    $this->requestsAnUltraSlowTimeConsumingMethod();

    return $someData;
}

I do not really need to wait for the finalisation of the $this->requestsAnUltraSlowTimeConsumingMethod() i just asynchronously want to say to this method to do stuff and return the result to customer.

Need to transform it in something like:

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

    async_method_call($this->requestsAnUltraSlowTimeConsumingMethod());

    return $someData;
}

Is there an async_method_call() in PHP ?

图片转代码服务由CSDN问答提供 功能建议

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

 公共函数myMethodForOptimisation(array $ options){
 //做一些事情
 
 $ this-> requestsAnUltraSlowTimeConsumingMethod(); 
 
返回$ someData; 
} 
    
 
 

我真的不需要等待 $ this-> requestsAnUltraSlowTimeConsumingMethod()的最终确定我只是异步想对此说 做事的方法和返回结果给客户。

需要将其转换为:

  public function myMethodForOptimisation(array $ options){
 //做一些事情
 
 async_method_call($ this-> requestsAnUltraSlowTimeConsumingMethod()); 
 
返回$ someData; 
} 
    
 
 

PHP中是否有async_method_call()?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douba6361 2015-02-10 13:25
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douti19680318 2015-02-10 13:14

    You may use threads: PHP threading call to a php function asynchronously

    This is quite only way to do so. there is several ways of using them, so choice the best/easiest for you.

    Im using this normally: http://php.net/manual/en/class.thread.php

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题