admin管理员组文章数量:1401176
I am trying to understand how to create a multi-process inside a loop in Workerman. And is it even possible to achieve this?
The goal is to have multiple processes run in parallel to iterate through an array. Can anyone help me with this?
Thank you for your assistance.
$worker = new \Workerman\Worker;
$worker->onWorkerStart = function () {
foreach($array as $item) {
$worker->count = 10;
// DO SOME STUFF
}
}
I am trying to understand how to create a multi-process inside a loop in Workerman. And is it even possible to achieve this?
The goal is to have multiple processes run in parallel to iterate through an array. Can anyone help me with this?
Thank you for your assistance.
$worker = new \Workerman\Worker;
$worker->onWorkerStart = function () {
foreach($array as $item) {
$worker->count = 10;
// DO SOME STUFF
}
}
Share
Improve this question
edited Mar 24 at 7:05
rozsazoltan
11k6 gold badges20 silver badges57 bronze badges
asked Mar 23 at 21:23
Евгений АнтиповЕвгений Антипов
5602 gold badges6 silver badges20 bronze badges
1 Answer
Reset to default 2Setting $worker->count
inside the loop won’t create new processes dynamically.
You need something similar to this.
$worker = new \Workerman\Worker();
$worker->count = 10; # Define here
$array = [...]; # data
$worker->onWorkerStart = function($worker) use ($array) {
$totalItems = count($array);
$perWorker = ceil($totalItems / $worker->count);
$workerId = $worker->id; # worker unique id
$start = $workerId * $perWorker;
$slice = array_slice($array, $start, $perWorker);
foreach ($slice as $item) {
# Process each item
}
};
\Workerman\Worker::runAll();
本文标签: workerPHP Workerman Multiple processes in foreach loopStack Overflow
版权声明:本文标题:worker - PHP Workerman. Multiple processes in foreach loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744268835a2598080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论