[文章作者:张宴 本文版本:v1.0 最后修改:2007.11.16 转载请注明出处:http://blog.zyan.cc]

  最近遇到一个问题,Linux下的PHP命令行程序作为守护进程,需要从队列文件中读一行数据,通过TCP协议发送给外地的接收服务器,再读下一行数据,再发送。当本地与外地的网络状况不好时,有时候发送一条数据所耗费的时间就较长,累积起来容易造成队列堵塞和延迟。

  于是,我准备用该PHP命令行程序生成多个子进程,将串行处理变成并行处理。最简单的方法就是在PHP中用exec()或popen()函数将一个shell命令行推到后台去执行,例如:
<?php
exec("/bin/sh /opt/zhangyan.sh &");
?>
  最后的&表示将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 /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.cc
tail -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”表示不显示标准输出和错误输出,最后的&表示推到后台执行。)

Tags: ,



技术大类 » PHP/JS/Shell | 评论(280) | 引用(4) | 阅读(98765)
Kamka Email
2023-1-16 12:41
Astonishing, What a Remarkable post. I tracked down this a ton of informatics. I was searching for it. I should propose you that compassionately keep on sharing such kind of info.If potential, Much appreciated.   Campingplatz Lago Maggiore
Joakmn Email
2023-1-17 22:52
You make such endless unprecedented concentrations here that I read your article on numerous occasions. Your points of view are according to my own for the most part. This is unimaginable substance for your perusers.   바카라
Jhgts Email
2023-1-18 16:35
Gratitude for carving out possibility to study this, I even have a corporation opinion approximately it and love studying greater in this problem. If conceivable, as you benefit aptitude, might you notice any issues with fresh your weblog with extra facts? It is rather beneficial for me.  Alpine Ice Hack
besthyiptemplate Email
2023-1-22 17:33
Do you want to buy a USA verified Paypal account? Or, want to get a PayPal verified account service from a trusted service provider? Have a Relax! First look at our words and make your decision. In today's online world most people are trying to make their business grow high   USA VERIFIED PAYPAL ACCOUNT  
Hibbah Email
2023-1-31 13:10
Ambking a brand new era of online slot video games in online casinos Looking for a novice slot website, slot video games, and actual pockets slots? If you're searching out it you then definitely have come to the proper location due to the fact we're a website. AMBKING, a main provider company in Thailand Just begun making use of slot wallets.  Ambking
genny Email Homepage
2023-3-31 19:07
A great marriage isn't something that just happens; it's something that must be created.” This helps keep your family out of trouble because different members may take this into account. While dating, both physically and culturally. We offer our marriage enrollment, marriage validation, and property enrollment companies. Marriage registration noidaMarriage registration Ghaziabad
genny roy Email Homepage
2023-3-31 19:09
In today’s era, finding your ideal companion is easier than you believe. So, give marital websites a shot if you’re looking for a committed relationship.We are a group of lawyers that provide sincere assistance with marriage registration in Noida, marriage registration in the city, and property registration in Noida.Marriage registration noidaMarriage registration Ghaziabad
dhamisingh Email Homepage
2023-4-1 16:13
Makers of nutrient enhancements have been made straightforward through agreement nutrient creation. The worker for hire holds a fair stock of crude fixings all together that you get opportune conveyances of your item at whatever point you put in your request. [Avvatar Isorich](https://powergenx.in/)
dhamisingh Email Homepage
2023-4-1 16:14
Makers of nutrient enhancements have been made straightforward through agreement nutrient creation. The worker for hire holds a fair stock of crude fixings all together that you get opportune conveyances of your item at whatever point you put in your request.[Avvatar Isorich](https://powergenx.in/)
genny Email Homepage
2023-4-3 16:43
The court marriage can be enrolled in the workplace of the marriage official in whose space/purview the lady or spouse dwells.We offer our marriage enrollment, marriage validation, and property enrollment companies. Marriage registration noidaMarriage registration Ghaziabad
slottoto Email Homepage
2023-5-13 15:28
yuk gabung bersama slottoto
cipit88 Email Homepage
2023-5-31 20:07
i really can understand what are you talking , cery informative and help me alot .. thankyou for that !!come and visit our website to slot cipit88
Riya jain Email Homepage
2023-8-7 17:21
A clean washroom reflects the professionalism and commitment to patient care that medical services offices strive for. It creates a positive impression on patients and visitors, fostering trust and confidence in the healthcare facility for Washroom cleaning services near me contact helperji company
Riya jain Email Homepage
2023-8-7 17:23
A clean washroom reflects the professionalism and commitment to patient care that medical services offices strive for. It creates a positive impression on patients and visitors, fostering trust and confidence in the healthcare facility. or If you are looking for  Washroom cleaning services near me or Bathroom cleaning services noida  then Helperji is the best in this  business.
riya Email Homepage
2023-8-31 19:35
Washroom cleaning has come a long way throughout history, reflecting not only advancements in technology but also changing attitudes toward hygiene and cleanliness. In this essay, we will explore the fascinating evolution of washroom cleaning practices, from ancient civilizations to the modern era and for besdeep cleaning services in noidacontact helperji
vsnutritions Email Homepage
2023-9-1 20:14
Optimizing your website's on-page elements is crucial for improving its visibility and search engine rankings. Pay attention to elements such as title tags, meta descriptions, heading tags, URL structure, and image alt text. Craft compelling and keyword-rich meta descriptions and title tags that encourage users to click on your search listings. Use descriptive headings to structure your content and make it easier for search engines and users to navigate. Optimizing these on-page elements helps search engines understand the relevance and context of your web pages, leading to improved rankings and increased organic traffic. know more: https://smiletechseo.com/”" target="_blank">seo agency new york
vsnutritions Email Homepage
2023-9-1 20:17
Technical SEO focuses on the technical aspects of your website to ensure it is search engine-friendly. Elements such as website speed, mobile-friendliness, crawlability, site structure, and URL optimization are essential for a smooth user experience and improved search engine rankings. Optimize your website\'s loading speed by compressing images, minimizing code, and leveraging browser caching. Ensure your website is mobile-friendly and responsive to accommodate the growing number of mobile users. Implement proper URL structure and internal linking to enhance site navigation and improve indexing by search engines. know more:  <a href=”https://smiletechseo.com/”>seo agency new york</a>
vsnutritions Email Homepage
2023-9-1 20:21
Backlinks play a crucial role in SEO as they signal to search engines that your website is reputable and trustworthy. Focus on building high-quality backlinks from authoritative websites in your industry. Develop relationships with relevant influencers and industry publications to earn valuable backlinks. Create link-worthy content that other websites would naturally want to reference and link to. Backlink building not only improves your website's search engine rankings but also drives referral traffic and increases your brand's visibility. know more: SEO AGENCY NEW YORK
分页: 13/15 第一页 上页 8 9 10 11 12 13 14 15 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]