swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 786 forks source link

关于httpClient co 的问题 #148

Closed aprchen closed 6 years ago

aprchen commented 6 years ago
Q A
Bug report? yes
Feature request? yes/no
swoft/http-client 1.0.1
Swoole version 2.1.1 (by php --ri swoole)
PHP version 7.1.4 (by php -v)
Runtime environment Mac

Details 使用httpclient 时,发一个请求,适配器选择 co会报图一的错误,选择swoole,coroutine则正确返回 而循环做并发请求的时候,会遇到图二的错误,是我的使用方法有问题吗?

图一 图二

Provide minimal script to reproduce the issue

 /**
     * @RequestMapping(route="/", method=RequestMethod::GET)
     */
    public function index()
    {
        $client = new Client([
            'adapter'=>'coroutine'
        ]);
        for ($i = 0; $i<100; $i++){
            /** @var HttpCoResult $request */
            $request = $client->get("http://www.baidu.com");
            $this->queue->push($request);
        }
        $result = Task::deliver("doRequest","run",[],Task::TYPE_ASYNC,0);
        return $result;
    }

  /**
     * @Inject()
     * @var RequestQueue
     */
    protected $queue;

   public function run(){
       App::profileStart("doRequest");
       $num = 0;
       while (true){
           $request = $this->queue->pop();
           if(!($request instanceof HttpCoResult)){
               break;
           }
           $num ++;
           $response = $request->getResponse();
           echo $response->getStatusCode()."\n" ;
       }
       App::profileEnd("doRequest");
       return $num;
   }
huangzhhui commented 6 years ago

第一个问题我这边没有重现,co 和 coroutine 都是一样的效果的; 第二个问题是Swoole的问题,将扩展更新至 2.1.2-alpha 可解决,此版本尚未发布,可通过 swoole/swoole-src 的 master 代码编译