账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    如何将一个数组分2批来运行coroutine处理
    • 2019-10-28 00:00
    • 11
    44
    0

    Swoole版本:4.2.1

    我想将一个数组分2批来运行coroutine处理,请问怎么实现呢?

    以下是我现在的代码逻辑,以下例子跑到一定数量的时候就会出问题,我企图通过分批运行来解决,而在实际的项目里,真正发生的问题是内存溢出了。
    实际项目中,我是在 Laravel 的命令行下跑任务的,然后下面例子中的 $list 有 3800+ 个元素,遍历每一个产生一个协程,每个协和走 HTTP 往公司的一个服务取数据后push到接收协程那里进行计算,但迭代到第 3700 个元素执行 go 函数产生协程时就报内存溢出了。

    $list = array_fill(0, 100000, 'test');  
    $listCount = count($list);  
    Coroutine::set(['max_coroutine' => $listCount + 1]);  
    $channel = new Channel(floor($listCount / 4));  
    go(function ()use($channel, $listCount){  
        $popCount = 0;  
        while($popCount < $listCount){  
            $value = $channel->pop();  
            $popCount++;  
            //...  
        }  
    });  
      
    foreach($list as $i => $item){  
        go(function()use($channel, $i, $item){  
            echo "第 {$i} 个协程在跑了\n";  
            try{  
    //          $data = 通过协程的Client请求些数据回来($item);  
                $cli = new \Swoole\Coroutine\Http\Client('www.qq.com', 80);  
                $cli->setHeaders([  
                    'Host' => "www.qq.com",  
                    "User-Agent" => 'Chrome/49.0.2587.3',  
                    'Accept' => 'text/html,application/xhtml+xml,application/xml',  
                    'Accept-Encoding' => 'gzip',  
                ]);  
                $cli->set([ 'timeout' => 1]);  
                $cli->get('/');  
                $data = $cli->body;  
                $cli->close();  
                $channel->push([  
                    'success' => true,  
                    'data' => $data,  
                ]);  
            }catch(\Exception $e){  
                $channel->push([  
                    'success' => false,  
                    'data' => $data,  
                ]);  
            }  
        });  
    }  
    swoole_event::wait();  
    echo 'over';
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 西瓜有点咸 普通会员 1楼

      在Python中,你可以使用concurrent.futures库来创建协程并分批运行。以下是一个例子:

      ```python import concurrent.futures

      创建一个任务列表,每个任务处理一个数组元素

      tasks = []

      定义一个函数,这个函数接受一个数组作为参数

      def process_array(arr): for i in arr: print(f"Processing element {i}")

      创建一个协程列表,每个协程处理一个任务列表中的一个任务

      with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: # 将每个任务列表中的任务添加到协程列表中 futures = {executor.submit(process_array, task) for task in tasks} # 等待所有协程完成 concurrent.futures.wait(futures) ```

      在这个例子中,我们首先创建了一个任务列表,每个任务都是一个函数,这个函数接受一个数组作为参数。然后,我们创建了一个协程列表,每个协程都是一个任务列表中的一个任务。最后,我们使用concurrent.futures.ThreadPoolExecutor来创建一个协程池,然后使用executor.submit来提交每个任务。最后,我们使用concurrent.futures.wait来等待所有协程完成。

      注意,这个例子使用了concurrent.futures.ThreadPoolExecutor来创建协程池,而不是concurrent.futures.ProcessPoolExecutorThreadPoolExecutor使用多线程来运行协程,而ProcessPoolExecutor使用进程来运行协程。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部