PHP中的多线程/并行处理 [英] Multithreading/Parallel Processing in PHP

查看:686
本文介绍了PHP中的多线程/并行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,将使用PHPExcel从MySQL数据库查询的数据生成报告。目前,它在处理中是线性的,它从MySQL获取数据,读入Excel模板,将数据写入模板,然后输出。我已经优化了代码,以至于数据只被迭代一次,而且在PHP端进行的处理非常少。查询以小于.001秒返回数百行,因此它运行足够快。在一些时间后,我发现我的瓶颈是(惊喜,惊喜)阅读模板和写输出。
我想这样做:

I have a PHP script that will generate a report using PHPExcel from data queried from a MySQL DB. Currently, it is linear in processing in that it gets the data back from MySQL, reads in the Excel template, writes the data to the template, then outputs it. I have optimized the code to the point that the data is only iterated over once, and there is very little processing done on the PHP side. The query returns hundreds of lines in less than .001 seconds, so it is running fast enough. After some timing I have found my bottlenecks to be (surprise, surprise) reading the template and writing the output. I would like to do this:

Spawn a thread/process to read the template
Spawn a thread/process to fetch the data
Return back to parent thread - Parent thread will wait until both are complete
Proceed on as normal

我的主要问题是这是可能的,是值得吗?如果是对两者,你会如何处理呢?
此外,它是CentOS上的PHP 5

My main questions are is this possible, is it worth it? If yes to both, how would you tackle it? Also, it is PHP 5 on CentOS

推荐答案

分叉Apache进程通常不是一个好主意。这可能会导致不确定的结果。相反,使用某种排队机制是更可取的。 Gearman是一个可以使用的开源排队机制。我也有一个博客文章在Zend服务器作业队列,谈论异步运行任务你排队吗? Zend服务器作业队列简介

It is generally not a good idea to fork an Apache process. That can cause undetermined results. Instead, using some kind of queuing mechanism is preferable. Gearman is an open source queuing mechanism you can use. I also have a blog post on the Zend Server Job Queue that talks about running tasks asynchronously Do you queue? Introduction to the Zend Server Job Queue.

您还可以使用Zend Framework Queuing类来实现一些异步工作。 Zend_Queue

You could also use something like the Zend Framework Queuing classes to implement some of the asynchronous work. Zend_Queue

@Swisstack,我也不同意你的说法,PHP不是为高性能而创建的。很少有语言特性导致性能低下。也许通过做不同语言之间比较$ a ++的原始语言测试你会看到,但这种类型的测试是无关紧要的。我已经做了几年的PHP咨询,我从来没有看到由于语言导致的性能问题。

@Swisstack, also I will disagree with your assertion that PHP is not created for high performance. Very seldom are language features the cause of slow performance. Perhaps by doing a raw language test comparing $a++ among different languages you will see that, but that type of testing is irrelevant. I've done consulting on PHP for several years and I have never seen a performance problem that was due to the language.

这篇关于PHP中的多线程/并行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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