[文章作者:张宴 本文版本: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 | 评论(278) | 引用(4) | 阅读(95747)
seo Email
2022-5-13 03:41
Great survey, I'm sure you're getting a great response. เล่นบาคาร่า เว็บไหนดี Pantip
seo Email
2022-5-19 02:06
You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here.  https://helvetia-facility.ch/
seo Email
2022-5-19 02:08
Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign.  https://helvetiatrans.ch/
seo Email
2022-5-19 02:10
Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home.  https://helvetiafacility.ch/
seo Email
2022-5-27 21:35
Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign.  Quantum AI Erfahrungen
seo Email
2022-5-31 05:18
I love seeing blog that understand the value of providing a quality resource for free.  small business loans alberta
seo Email
2022-6-1 04:31
The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface.  kosmetiker wien
NBHGU Email
2022-6-2 12:56
The bricklayer is exposed to immoderate physical conditions. Too masses heat, too much humidity, immoderate noise and vibrations, contact with chemical agents. Well, Brazilian guidelines gives for a completely unique retirement magnificence for professionals who artwork beneathneath wonderful conditions.   como funcionaaposentadoria do pedreiro
ZSDXF Email
2022-6-3 01:35
You do now no longer want to worry anymore. Buy Real Legal Online Registered Driver 's License of desired Category on this net web website online with Confidence and Security at the side of Driver's License signed via Department of Transport.  deutschen führerschein kaufen
seo Email
2022-6-5 03:14
I would like to say that this blog really convinced me to do it! Thanks, very good post.  คาสิโนออนไลน์เว็บตรง
สล็อต1234 Email
2022-6-8 19:02
You can enjoy playing games together with online sites. And thanks to your website for letting us participate.สล็อต1234
pgสล็อต Email
2022-6-8 19:04
I got too much intriguing things on your blog sitepgสล็อต
สล็อต888 Email
2022-6-8 19:05
Thank you for posting such a great article! I found your website perfect for my needs.สล็อต888
Hibbah Email
2022-6-9 23:41
For the ones of you which have attended our Foundation and Advanced Botox and Dermal Fillers courses, you may be conscious that dermal fillers are used to introduce quantity lower back into regions of the face which have misplaced quantity throughout the growing older process.   buy Botox uk
seo Email
2022-6-12 03:23
Thank you because you have been willing to share information with us.   อ่านการ์ตูนฟรี
seo Email
2022-6-21 16:45
I just couldn't leave your website before telling you that I truly enjoyed the top quality. g2g สล็อต
ฝาก 99 รับ 300
2022-6-26 11:24
<a href="https://jokergaming-789.com/ฝาก-99-รับ-300/">ฝาก 99 รับ 300</a> Online Slot Joker 789, the leading mobile game that meets international standards. It is a slot game system with new slot games. that has been developed all the time
fin88 slot
2022-6-26 11:24
<a href="https://jokergaming-789.com/fin88-slot/">fin88 slot</a> jokergaming 789 slot game provider (slot) casino online slots Play online slots via the websitemakes playing your online gambling games easier
seo Email
2022-6-26 15:44
You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers.  ดิไอคอนกรุ๊ป
seo Email
2022-7-2 03:13
Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work.  Room fiberry
分页: 8/15 第一页 上页 3 4 5 6 7 8 9 10 11 12 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]