PHP中的并行处理-如何做到这一点? [英] Parallel processing in PHP - How do you do it?

查看:115
本文介绍了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

但是我认为这两种方式会增加在队列上创建批处理的开销,应该主要在后台运行?

But I think those 2 ways will add pretty much overhead for creating batch processing on a queue taht should mainly run on the background?

我还阅读了有关pcntl_fork的信息,这似乎也是解决该问题的一种方法.但是,如果您真的不知道自己在做什么(就像现在的我一样;),那看起来就会变得很混乱.

I also read about pcntl_fork wich 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中的并行处理-如何做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆