[文章作者:张宴 本文版本: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”表示不显示标准输出和错误输出,最后的&表示推到后台执行。)
Hibbah
2022-4-9 17:14
You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this... Online Casino Singapore
seo
2022-4-10 18:23
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. مسلسل المشوار
seo
2022-4-10 19:41
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. 인천출장안마
Hibbah
2022-4-15 00:05
If you are looking for more information about flat rate locksmith Las Vegas check that right away. biuro nieruchomosci szczecin
seo
2022-4-16 16:55
I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. 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. rapid prototyping services
Hibbah
2022-4-17 00:41
Hi there! Nice stuff, do keep me posted when you post again something like this! noticias
Hibbah
2022-4-19 23:47
We are a 100% prison Thai on line income organization, 100% bt unfastened organization with international elegance excellent products. It is an modern product from Far Infrared Glutathione Complex Innovation that has obtained global awards. the icon group
SDKM
2022-4-22 19:24
Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any suggestions? Many thanks! مسلسل المداح 2 اسطورة الوادي
seo
2022-4-22 21:11
What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. trivago hotels
JNKA
2022-4-23 16:54
Your blog is too much amazing. I have found with ease what I was looking. Moreover, the content quality is awesome. Thanks for the nudge! ellevet mobility chews
seo
2022-4-23 17:15
Awesome blog. I enjoyed reading your articles. slot deposit dana
Sweet
2022-4-24 00:40
What a thrilling post, you have pointed out some excellent points, I as well believe this is a superb website. I have planned to visit it again and again. picpay recarga jogo
seo
2022-4-27 06:31
I simply wanted to say thanks once again. I am not sure the things I would’ve made to happen in the absence of the pointers discussed by you over such area. Entirely was an absolute scary concern in my position, but being able to view the well-written avenue you solved that forced me to jump for fulfillment. Now i am grateful for your assistance and as well , hope you are aware of a great job you happen to be accomplishing instructing people today using your websites. I am certain you have never encountered any of us. 광명출장안마
seo
2022-4-27 19:12
Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. Android Phones
Hibbah
2022-4-27 23:49
Retirement primarily based on contribution time generally increases more doubts. Because of this, Quem tem direito de retirada por tempo de contribuição
seo
2022-4-30 03:23
Umzugsfirma ZürichZügelfirma für sorglose Umzugsservice – Helvetia Transporte & Umzüge AGIhre Umzugsfirma für den Kanton Zürich, Aargau, Luzern, Zug und schweizweit oder für Auslandsumzüge.Umzüge aller Art, Umzugsreinigung mit Abnahmegarantie, Möbel Lagerungen, Fachgerechte Entsorgung und Räumungen sind unsere Kerndienstleistungen.Gerne beraten wir Sie als Privat- und Firmenkunde bei Ihrem Vorhaben. Umzugsfirma Zürich
VLOPA
2022-5-2 20:14
I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer.Thanks. handicap placard form
seo
2022-5-2 22:30
Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. talk to strangers online video
SKNGA
2022-5-7 11:53
Your blog is too much amazing. I have found with ease what I was looking. Moreover, the content quality is awesome. Thanks for the nudge! https://www.handicapmd.com/handicap-parking-blog/how-to-get-a-disabled-parking-permit-online-in-california
Hibbah
2022-5-8 16:34
I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing. trans friendly
分页: 7/15 2 3 4 5 6 7 8 9 10 11