[文章作者:张宴 本文版本:v1.0 最后修改:2007.11.16 转载请注明出处:http://blog.zyan.cc]
最近遇到一个问题,Linux下的PHP命令行程序作为守护进程,需要从队列文件中读一行数据,通过TCP协议发送给外地的接收服务器,再读下一行数据,再发送。当本地与外地的网络状况不好时,有时候发送一条数据所耗费的时间就较长,累积起来容易造成队列堵塞和延迟。
于是,我准备用该PHP命令行程序生成多个子进程,将串行处理变成并行处理。最简单的方法就是在PHP中用exec()或popen()函数将一个shell命令行推到后台去执行,例如:
但是这样会有一个问题,如果推到后台的进程太多,可能会导致服务器系统资源耗尽而崩溃,所以必须控制进程数量。
我写了一个PHP程序(/opt/zhangyan.php)、一个shell程序(/opt/zhangyan.sh)作为测试用例。
程序的逻辑:
1、设置/opt/zhangyan.php最多允许生成500个子进程;
2、当/opt/zhangyan.php读取到一条数据后,将允许生成的子进程数减1(空闲进程数$p_number=500-1=499),然后将数据交给/opt/zhangyan.sh去后台处理,不等待/opt/zhangyan.sh处理结束,继续读取下一条数据;
3、当允许生成的子进程数减至0时(空闲进程数$p_number=0),/opt/zhangyan.php会等待1秒钟,然后检查后台还有多少个/opt/zhangyan.sh子进程尚未处理结束;
4、如果1秒钟之后/opt/zhangyan.php发现后台的/opt/zhangyan.sh子进程数还是500(空闲进程数$p_number=0),会继续等待1秒钟,如此反复;
5、如果/opt/zhangyan.php发现后台尚未处理结束的/opt/zhangyan.sh子进程数减少到300个了(空闲进程数$p_number=500-300=200),那么/opt/zhangyan.php会再往后台推送200个/opt/zhangyan.sh子进程;
/opt/zhangyan.php代码如下: (/opt/zhangyan.php程序用来模拟从队列文件中读取1000行数据,交给子进程/opt/zhangyan.sh去处理。)
/opt/zhangyan.sh代码如下: (/opt/zhangyan.sh脚本用来模拟向外地接收服务器发送数据。其中的$(expr $RANDOM % 4 + 1)用来生成1~5之间的随机数,用来使程序暂停1~5秒钟。暂停1秒表示网络状况好,发送数据顺畅;暂停2~6秒表示网络状况不好,发送过程需要1~5秒。)
执行程序:
(/usr/local/php/bin/php因PHP解析器所在的路径)
查看/opt/zhangyan.sh打下的日志文件的第一行和最后一行:
可以看出,500进程并发处理这1000条数据只耗费5秒钟。而按照原来的串行模式,处理每条数据即使只耗费最短的1秒钟,也需要1000秒,约合16分钟才能完成。
PS:将PHP程序作为Linux守护进程的方法:
(nohup命令可以在用户退出终端后仍然执行程序,“2>&1 > /dev/null”表示不显示标准输出和错误输出,最后的&表示推到后台执行。)
最近遇到一个问题,Linux下的PHP命令行程序作为守护进程,需要从队列文件中读一行数据,通过TCP协议发送给外地的接收服务器,再读下一行数据,再发送。当本地与外地的网络状况不好时,有时候发送一条数据所耗费的时间就较长,累积起来容易造成队列堵塞和延迟。
于是,我准备用该PHP命令行程序生成多个子进程,将串行处理变成并行处理。最简单的方法就是在PHP中用exec()或popen()函数将一个shell命令行推到后台去执行,例如:
<?php
exec("/bin/sh /opt/zhangyan.sh &");
?>
最后的&表示将shell脚本推到后台去执行。exec("/bin/sh /opt/zhangyan.sh &");
?>
但是这样会有一个问题,如果推到后台的进程太多,可能会导致服务器系统资源耗尽而崩溃,所以必须控制进程数量。
我写了一个PHP程序(/opt/zhangyan.php)、一个shell程序(/opt/zhangyan.sh)作为测试用例。
程序的逻辑:
1、设置/opt/zhangyan.php最多允许生成500个子进程;
2、当/opt/zhangyan.php读取到一条数据后,将允许生成的子进程数减1(空闲进程数$p_number=500-1=499),然后将数据交给/opt/zhangyan.sh去后台处理,不等待/opt/zhangyan.sh处理结束,继续读取下一条数据;
3、当允许生成的子进程数减至0时(空闲进程数$p_number=0),/opt/zhangyan.php会等待1秒钟,然后检查后台还有多少个/opt/zhangyan.sh子进程尚未处理结束;
4、如果1秒钟之后/opt/zhangyan.php发现后台的/opt/zhangyan.sh子进程数还是500(空闲进程数$p_number=0),会继续等待1秒钟,如此反复;
5、如果/opt/zhangyan.php发现后台尚未处理结束的/opt/zhangyan.sh子进程数减少到300个了(空闲进程数$p_number=500-300=200),那么/opt/zhangyan.php会再往后台推送200个/opt/zhangyan.sh子进程;
/opt/zhangyan.php代码如下: (/opt/zhangyan.php程序用来模拟从队列文件中读取1000行数据,交给子进程/opt/zhangyan.sh去处理。)
/opt/zhangyan.sh代码如下: (/opt/zhangyan.sh脚本用来模拟向外地接收服务器发送数据。其中的$(expr $RANDOM % 4 + 1)用来生成1~5之间的随机数,用来使程序暂停1~5秒钟。暂停1秒表示网络状况好,发送数据顺畅;暂停2~6秒表示网络状况不好,发送过程需要1~5秒。)
执行程序:
/usr/local/php/bin/php /opt/zhangyan.php
(/usr/local/php/bin/php因PHP解析器所在的路径)
查看/opt/zhangyan.sh打下的日志文件的第一行和最后一行:
head -n 1 /opt/zhangyan.log
2007-11-16 07:54:13 http://blog.zyan.cctail -n 1 /opt/zhangyan.log
2007-11-16 07:54:18 http://blog.zyan.cc可以看出,500进程并发处理这1000条数据只耗费5秒钟。而按照原来的串行模式,处理每条数据即使只耗费最短的1秒钟,也需要1000秒,约合16分钟才能完成。
PS:将PHP程序作为Linux守护进程的方法:
nohup /usr/local/php/bin/php /opt/zhangyan.php 2>&1 > /dev/null &
(nohup命令可以在用户退出终端后仍然执行程序,“2>&1 > /dev/null”表示不显示标准输出和错误输出,最后的&表示推到后台执行。)
aree
2022-9-18 14:35
Super-Duper site! I am Loving it!! Will come back again, Im taking your feed also, Thanks. <a href="http://gzymsy24.pl/allegro/inc/gry_hazardowe_w_polsce___gdzie_i_jak_gra__bezpiecznie_1.html">http://gzymsy24.pl/...
AREE
2022-9-18 14:36
Super-Duper site! I am Loving it!! Will come back again, Im taking your feed also, Thanks. http://gzymsy24.pl/allegro/inc/gry_hazardowe_w_polsce___gdzie_i_jak_gra__bezpiecznie_1.html
AREE
2022-9-18 14:37
Super-Duper site! I am Loving it!! Will come back again, Im taking your feed also, Thanks.http://gzymsy24.pl/allegro/inc/gry_hazardowe_w_polsce___gdzie_i_jak_gra__bezpiecznie_1.html
seo
2022-9-18 16:14
Great things you’ve always shared with us. Just keep writing this kind of posts.The time which was wasted in traveling for tuition now it can be used for studies.Thanks ครูปันอายุน้อยร้อยล้าน
are
2022-9-19 23:08
If you're interested in free interior design courses, the Home Design Institute in Paris is a great place to start. With experienced faculty and a range of course offerings, the Institute can help you develop the skills you need to pursue a career in interior design. From basics like color theory and furniture selection to more advanced topics like space planning and lighting design, the Institute's free courses cover everything you need to know to start your own business or take your career to the next level. And with no obligations or commitments, you can take as many or as few courses as you like. So why not get started today? Visit the Home Design Institute's website to learn more.free interior design courses
seo
2022-9-21 03:12
Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.Please continue this great work and I look forward to more of your awesome blog posts. كورة لايف
aa
2022-9-21 15:26
Super-Duper site! I am Loving it!! Will come back again, Im taking your feed also, Thanks. <a href="https://sites.google.com/view/merchant-services-iso-agent/home">credit card processing residual income</a>
are
2022-9-22 14:51
It is a great website.. The Design looks very good.. Keep working like that!. lentor modern
seo
2022-9-25 04:33
Thank you because you have been willing to share information with us. Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks เว็บสล็อตเว็บตรง
seo
2022-9-26 20:31
If possible, as you gain expertise, would you mind updating your blog with more information. It is extremely helpful for me.I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. สล็อต ยืนยัน otp รับเครดิตฟรี100
seo
2022-9-27 04:44
This type of message always inspiring and I prefer to read quality content, so happy to find good place to many here in the post, the writing is just great, thanks for the post.Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks kora star
seo
2022-9-28 17:35
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. slot
seo
2022-9-28 23:36
I love seeing blog that understand the value of providing a quality resource for free.You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming.We have sell some products of different custom boxes.it is very useful and very low price please visits this site thanks and please share this post with your friends. สล็อตเครดิตฟรี
seo
2022-10-6 11:59
Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.Please continue this great work and I look forward to more of your awesome blog posts. SSyoutube
DD
2022-10-6 12:15
Deutscher FührerscheinWARUM ERHALTEN SIE DIE DEUTSCHE FAHRERLIZENZ VON UNS?Unsere Online-Agentur wurde als beste Adresse für den Führerschein in Deutschland ausgezeichnet. Darüber hinaus verwenden wir bei der Herstellung von Führerscheinen hochwertige Technologien und Materialien. Sie müssen sich um nichts kümmern, wenn Sie sich an unsere Online-Agentur wenden, um einen Führerschein in Deutschland zu kaufen. FührerscheinSie möchten wissen, wo Sie in Deutschland einen Führerschein machen können? Unsere Online-Agentur hilft Ihnen dabei. Darüber hinaus helfen wir Ihnen, die beste Arbeitsqualität zu erreichen. Unser Team unterstützt Sie bei Ihren geplanten Anforderungen. Wir werden Ihre Bestellung innerhalb der zugesagten Fristen ausführen. Sie müssen uns lediglich Ihre Bestelldaten mitteilen. Kontaktieren Sie uns jetzt!Stellen Sie eine Anfrage. Führerscheindeutscher führerscheineingetragenen führerschein kaufenfake führerschein erkennenführerschein kaufen kontaktführerschein österreichführerscheinnummer deutschlandführerscheinstelle österreichid nummer führerscheinrosa führerschein österreichBestellen Sie bei uns mehr als nur einen österreichischen FührerscheinÖsterreich FührerscheinWARUM ERHALTEN SIE VON UNS EINE AUSTRAINISCHE FÜHRERLIZENZ?Unsere Online-Agentur wurde als beste Adresse für den Führerschein in Österreich ausgezeichnet. Bei der Herstellung von Führerscheinen verwenden wir hochwertige Technologien und Materialien. Sie müssen sich um nichts kümmern, wenn Sie sich an unsere Online-Agentur wenden, um einen Führerschein in Österreich zu kaufen.Sie möchten wissen, wo Sie in Österreich einen Führerschein machen können? Unsere Online-Agentur hilft Ihnen dabei. Wir helfen Ihnen, die beste Arbeitsqualität zu erreichen. Unser Team unterstützt Sie bei Ihren geplanten Anforderungen. Wir werden Ihre Bestellung innerhalb der zugesagten Fristen ausführen. Sie müssen uns lediglich Ihre Bestelldaten mitteilen. Kontaktieren Sie uns jetzt!Stellen Sie eine Anfrage. https://fuhrerscheinstelle.com/https://www.onlinedeutschefuhrerschein.com/https://dayanfahrschule.com/https://fuhrersche... +49 1512 4940724.
seo
2022-10-10 23:01
Slot5000 adalah agen gacor deposit 5000 sudah bisa bermain di situs 303 slot 5000 login terpercaya yang sangat mudah menang dengan satu akun user id. Slot 5000
alam
2022-10-16 18:55
This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. <a href="https://gacorslot168.info/">gacorslot</a>
seo
2022-10-18 05:52
Great survey, I'm sure you're getting a great response.You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here.This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs…Really I enjoy your site with effective and useful information. It is included very nice post with a lot of our resources.thanks for share. i enjoy this post.Thank you because you have been willing to share information with us. บาคาร่า 888
seo
2022-10-20 02:37
I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. 카지노사이트
seo
2022-10-24 02:20
Thank you because you have been willing to share information with us. Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks slot
分页: 11/15 6 7 8 9 10 11 12 13 14 15