[文章作者:张宴 本文版本: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”表示不显示标准输出和错误输出,最后的&表示推到后台执行。)
seo
2022-10-24 02:23
I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject.Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. สล็อตโจ๊กเกอร์
seo
2022-10-24 02:25
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.Nice knowledge gaining article. This post is really the best on this valuable topic. slotxo
seo
2022-10-24 02:27
i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful.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...We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work.i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful.Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home. ทดลองเล่นสล็อต pragmatic play
seo
2022-10-24 20:00
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.I admit, I have not been on this web page in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues. spinix
seo
2022-10-24 20:01
Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign.Nice post! This is a very nice blog that I will definitively come back to more times this year! I was reading some of your content on this website and I conceive this internet site is really informative ! Keep on putting up.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.I wil be checking back soon to find out what additional posts you include. บุหรี่ไฟฟ้า
powergenx
2022-11-1 16:27
Powergenx is India’s largest online health & fitness store for men and women. Shop online from the latest collections of health, fitness and similar products featuring the best brands.Know more: Supplement Store Onlinewhey protein powderMass gainer
rere
2022-11-9 16:36
https://www.youtube.com/<a href="https://www.youtube.com/">https://www.youtube.com/</a>
waktogel
2022-11-9 16:39
Thanks for the informative information sharing with us. Keep writing.https://prediksiwaktogel.com
Professional Deep Cleaning Services
2022-11-9 16:57
We are the perfect locations for you in the event that you are searching for a dependable Homegrown partner to lessen your endless house errands and save a ton of time. It is critical to painstakingly pick your homegrown assistant. As some unacceptable decision can be an all out cerebral pain for your home and family.KNOW MORE:<a href="https://www.helperji.in/">Professional Deep Cleaning Services</a><a href="https://www.helperji.in/">Kitchen Cleaning in Noida</a>
Professional Deep Cleaning Services
2022-11-9 16:58
We are the perfect locations for you in the event that you are searching for a dependable Homegrown partner to lessen your endless house errands and save a ton of time. It is critical to painstakingly pick your homegrown assistant. As some unacceptable decision can be an all out cerebral pain for your home and family.KNOW MORE: Professional Deep Cleaning Services | Kitchen Cleaning in Noida | cleaning services in delhi | home cleaning services delhi | house deep cleaning service near me
Professional Deep Cleaning Services
2022-11-9 16:59
We are the perfect locations for you in the event that you are searching for a dependable Homegrown partner to lessen your endless house errands and save a ton of time. It is critical to painstakingly pick your homegrown assistant. As some unacceptable decision can be an all out cerebral pain for your home and family.KNOW MORE:Professional Deep Cleaning Services
sexybaccarat
2022-11-13 05:06
<a href="https://www.lava879.com/">สล็อต เว็บตรงไม่ผ่านเอเย่นต์ไม่มีขั้นต่ํา วอเลท</a> เล่นง่ายได้ไว้ ไม่ล็อกยูสฝาก-ถอนออโต้ Lava879
seo
2022-11-22 20:16
Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. เล่นพนัน เว็บไหนดี
Hibbah
2022-11-29 12:21
This is a tremendous publish. I like this topic.This web website online has masses of advantage.I determined many exciting matters from this web website online. It facilitates me in lots of ways.Thanks for posting this once more. 플레이포커 머니상
Hibbah
2022-11-30 17:28
Therefore gathering web sites and programs For gambling on line slots as a great deal as viable best right here We assure that Every internet site we pick All of them are expert Slot on line carrier provider, smooth to access, convenient, and safe, permitting you to play 888 on line slots from everywhere withinside the world. No rely what day or time you need to play. สล็อต888
AMNKJU
2022-12-1 21:54
This permits a well disposed climate for poker players of all expertise levels on the grounds that the obscurity keeps sharks from focusing on unambiguous poker players. There is no poker game history for different players to dissect. 플레이포커 머니상
Hibah
2022-12-2 10:09
It's no obscure reality that the style undertaking has an extremely terrible impact at the climate. From unscrupulously obtained difficult work to pollutive creation rehearses, rapid style reasons irreversible mischief to the planet. <a href="https://bezen.eco/fast-fashion-brands-to-avoid/">Fast Fashion Brands To Avoid</a> Fast Fashion Brands To Avoid
Jojoakon
2022-12-2 21:49
At G2G, players have access to a wide variety of slot machines that offer some of the best payouts in the industry. Not only do they offer large jackpots, but they also feature numerous bonus rounds, free spins, and other great incentives. Additionally, G2G slots allow players to play with real money as well as virtual currency, giving players the freedom to choose how they want to play. g2g888
Hibbah
2023-1-12 17:35
Group Buy search engine marketing Tools actually best, with limitless capability and limitless support. Committed to presenting the cheapest, a maximum solid provider in comparison to all different search engine marketing device vendors withinside the world. You will shop lots of bucks whilst you be a part of Group search engine marketing Tools. group buy seo tools
Kmbhg
2023-1-14 22:27
Super site! I'm Esteeming it!! Will return again, Im taking your feed furthermore, Thankful. speedtest
分页: 12/15 7 8 9 10 11 12 13 14 15