doubi2145 2011-05-24 07:56
浏览 74
已采纳

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.

E.g. doing the parallel processing by calling a script several times through fsockopen like explained here:
Easy parallel processing in 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?

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 ;)).

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

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?

Thanks for any input.

  • 写回答

7条回答 默认 最新

  • doubi1713 2011-05-24 08:19
    关注

    i use exec(). Its easy and clean. You basically need to build a thread manager, and thread scripts, that will do what you need.

    I dont like fsockopen() because it will open a server connection, that will build up and may hit the apache's connection limit

    I dont like curl functions for the same reason

    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...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集