Parallel processing in PHP - How do you do it?(PHP 中的并行处理 - 你是怎么做的?)
问题描述
我目前正在尝试在 php 中实现一个作业队列.然后队列将作为批处理作业进行处理,并且应该能够并行处理一些作业.
I am currently trying to implement a job queue in php. The queue will then be processed as a batch job and should be able to process some jobs in parallel.
我已经做了一些研究并找到了几种实现方法,但我并没有真正了解它们的优缺点.
I already did some research and found several ways to implement it, but I am not really aware of their advantages and disadvantages.
例如通过 fsockopen
多次调用脚本来执行并行处理,如下所述:
PHP 中的简单并行处理
E.g. doing the parallel processing by calling a script several times through fsockopen
like explained here:
Easy parallel processing in PHP
我发现的另一种方法是使用 curl_multi
函数.
curl_multi_exec PHP 文档
Another way I found was using the curl_multi
functions.
curl_multi_exec PHP docs
但我认为这 2 种方法会为在主要在后台运行的队列上创建批处理增加相当多的开销?
But I think those 2 ways will add pretty much overhead for creating batch processing on a queue that should mainly run on the background?
我还阅读了有关 pcntl_fork
的文章,这似乎也是一种处理问题的方法.但是,如果您真的不知道自己在做什么(就像我现在这样),这看起来会变得非常混乱.
I also read about pcntl_fork
which also seems to be a way to handle the problem. But that looks like it can get really messy if you don't really know what you are doing (like me at the moment).
我还查看了 Gearman
,但在那里我还需要根据需要动态生成工作线程,而不仅仅是运行一些然后让 Gearman 作业服务器将其发送到免费工人.特别是因为线程应该在执行完一项作业后干净利落地退出,以免遇到最终的内存泄漏(该问题的代码可能并不完美).
Gearman 入门
I also had a look at Gearman
, but there I would also need to spawn the worker threads dynamically as needed and not just run a few and let the gearman job server then sent it to the free workers. Especially because the threads should be exit cleanly after one job has been executed, to not run into eventual memory leaks (code may not be perfect in that issue).
Gearman Getting Started
所以我的问题是,你如何处理 PHP 中的并行处理?为什么选择你的方法,不同的方法可能有哪些优点/缺点?
So my question is, how do you handle parallel processing in PHP? And why do you choose your method, which advantages/disadvantages may the different methods have?
推荐答案
我使用 exec()
.它简单而干净.您基本上需要构建一个线程管理器和线程脚本,这将满足您的需求.
i use exec()
. Its easy and clean. You basically need to build a thread manager, and thread scripts, that will do what you need.
我不喜欢 fsockopen()
因为它会打开一个服务器连接,这会建立起来并可能达到 apache 的连接限制
I dont like fsockopen()
because it will open a server connection, that will build up and may hit the apache's connection limit
出于同样的原因,我不喜欢 curl
函数
I dont like curl
functions for the same reason
我不喜欢 pnctl
因为它需要可用的 pnctl 扩展,而且你必须跟踪父/子关系.
I dont like pnctl
because it needs the pnctl extension available, and you have to keep track of parent/child relations.
从来没有玩过齿轮...
never played with gearman...
这篇关于PHP 中的并行处理 - 你是怎么做的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 中的并行处理 - 你是怎么做的?
基础教程推荐
- 使用 PDO 转义列名 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01