[文章作者:张宴 本文版本: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”表示不显示标准输出和错误输出,最后的&表示推到后台执行。)
kamrongill
2023-9-18 15:46
Whether you Online Matka Play or one more kind of wagering game, knowing the principles and how the game works is significant. Other than the nuts and bolts, you ought to likewise know how to apply these guidelines to augment your possibilities of winning. In the event that you feel a little unsure about the principles of the game, you can track down a lot of web-based assets to help you.
devis
2023-10-3 19:39
If you want to play a teen Patti game and want a good bonus sign up. Visit us: Teen Patti
Best BCAA supplement
2023-11-7 16:01
Three essential amino acids make up the branched-chain amino acids (BCAAs): leucine, isoleucine, and valine. BCAA supplements are essential, meaning that they cannot be made by the body and need to be obtained from food. KNOW MORE: Best BCAA supplementBest BCAA Brand
Probyto
2023-11-28 16:50
If you are looking for Machine Learning Models and Machine Learning Models deployment services then probyto is the best company in the market. KNOW MORE: Machine Learning ModelsMachine Learning Models deployment
Lucky cola
2024-2-7 16:42
Join the gaming community and unleash your competitive spirit. Lucky cola
jackk
2024-4-1 15:31
Having the appropriate supplements can make all the difference when it comes to muscle building and strength gains. With Rc King Mass Gainer, you are investing in your fitness journey rather than just a product. At Powergenx, you may experience the power of RC King and discover your ultimate potential.Rc King Mass Gainer
SIMRAN
2024-4-1 16:04
Leading provider of business intelligence services in the industry, Probyto AI has the know-how and experience to support your success. The most prominent supplier of corporate reporting and data visualization services in the market is Probyto AI. <a href="https://probyto.com/bi-reporting/">Business Intelligence services</a>
PSO Security Services
2024-4-1 17:55
I can state with confidence that Helperji PSO Security Services are unmatched, having been a satisfied client. They stand out from other providers because to their meticulous attention to detail and proactive security approach. Their staff goes above and above to provide peace of mind, from the first consultation to on-site security. If you\'re looking for trustworthy security solutions, I\'d suggest Helperji without a second thought.
simrankaur
2024-4-20 21:16
Business intelligence consulting involves more than simply math calculations; it also involves turning data into insights that can be put to use and improve company performance. Businesses may take advantage of new opportunities, reduce risks, and outperform rivals in today's quickly changing economy by utilizing Probyto's experience and commitment.https://probyto.com/bi-reporting/
Rc Pro Antium
2024-4-22 13:25
Consuming a caloric surplus—consuming more calories than one expels—is one of the cornerstones of building muscle growth. Mass gainers provide a practical and effective technique to boost calorie intake without feeling unduly full or bloated for novices who find it difficult to eat enough to fulfill their caloric demands, especially those with rapid metabolisms. KNOW MORE : Rc king Mass GainerRc king Mass Gainer
mridul2024
2024-4-22 15:28
Helperji appears to be the ideal choice for my need for trustworthy office cleaning services in Noida. Their crew distinguishes itself from the competition with their meticulous attention to detail and dedication to using safe cleaning chemicals. Their open pricing policy and accommodating schedule options really excite me. I'm eager to use their services myself.office cleaning services in Noida
mridul2024
2024-4-22 15:29
I've been looking for trustworthy commercial cleaning services for offices in Noida, and Helperji appears to be the right choice. Their team distinguishes themselves from the competition with their meticulous attention to detail and dedication to using safe cleaning chemicals. Their clear pricing structure and accommodating schedule options really impress me. I'm eager to use their services for myself.commercial cleaning services
simrankaur
2024-4-29 18:07
Through in-depth analysis and scenario modeling, business intelligence consulting helps organizations identify and mitigate risks, ensuring resilience in the face of uncertainty. KNOW MORE: https://probyto.com/bi-reporting/
Rc Pro Antium
2024-4-29 19:25
rc pro antium meets your various fitness objectives. It's a fantastic post-workout smoothie for replenishing depleted glycogen stores and accelerating muscle recovery.
Rc Pro Antium
2024-4-30 14:36
Protein is vital, as is a well-balanced diet rich in carbs, fats, vitamins, and minerals. Each vitamin plays a distinct function in general health, and a well-balanced diet is essential for addressing your body's requirements. KNOW MORE : Rc King Mass gainer
mridul2024
2024-4-30 19:55
I recognize the value of keeping a spotless workplace as a professional with a foundation in Noida. It is encouraging to see Helperji's focus on commercial cleaning services in Noida, since it shows a thorough comprehension of the particular requirements of our area. eager to witness Helperji's potential influence on our work environment.
mridul2024
2024-4-30 19:58
I know how important it is to keep a spotless office space because I work as a professional in Noida. It is encouraging that Helperji specializes in office cleaning services in Noida , as it shows a thorough comprehension of the particular requirements of our city. Looking forward to seeing how Helperji affects our workstation
simrankaur
2024-5-1 13:13
Probyto\'s business intelligence services have helped a great deal of clients in a variety of industries accomplish important business goals. Our solutions have positively impacted our clients\' businesses in a real way, helping them achieve their goals of increasing customer satisfaction, increasing revenue, and obtaining a competitive edge. KNOW MORE: https://probyto.com/bi-reporting/
Business
2024-5-8 15:49
Beyond descriptive reporting, Probyto provides advanced analytics solutions. Probyto Business Intelligence Services identifies hidden patterns, trends, and correlations within the data by utilizing techniques like predictive modeling, machine learning, and artificial intelligence. KNOW MORE: Business Intelligence services
mridul2024
2024-5-9 15:34
When it comes to providing clients with the highest level of professionalism in client protection, PSO Security Services definitely standss out. For both individuals and companies, Helperji's all-encompassing strategy guarantees unmatched protection. Their team's proficiency in PSO security tactics, along with their cutting-edge technological capabilities, establishes them as a reliable option in the sector. Helperji's commitment to providing their clients with peace of mind is evident in every step of the process, from risk assessment to implementation. It's comforting to know that Helperji's PSO Security Services puts safety first. Know More: PSO Security Services || Personal Security Services
分页: 14/15 9 10 11 12 13 14 15