[文章作者:张宴 本文版本: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) | 阅读(98753)
Hibbah Email
2021-12-14 13:44
When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style.  Exipure
Hibbah Email
2021-12-14 21:24
I gotta  favorite this website   it seems  very helpful .  Gebäudereinigung Stuttgart
Hibah Email
2021-12-15 18:42
Your articles are inventive. I am looking forward to reading the plethora of articles that you have linked here. Thumbs up!   superslot
bandar togel terperrcaya Email Homepage
2021-12-16 11:28
Today, I was just browsing along and came upon your blog. Just wanted to say good blog and this article helped me a lot, due to which I have found exactly I was looking. <a href="http://www.thakasuem.go.th/datas/bandar-togel-terpercaya">bandar togel terpercaya</a>
Hibbah Email
2021-12-16 17:47
i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful.  How To Be Born Again
seoo Email
2021-12-16 22:25
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.  รีแพร์
Hibbah Email
2021-12-20 23:02
I cannot wait to dig deep and kickoff utilizing resources that I received from you. Your exuberance is refreshing.  Limo Service near me
Jojoakon Email
2021-12-22 22:01
I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks.   12bet
Jojokhan Email
2021-12-27 16:04
This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you.   Dankwoods blunt
seoo Email
2022-1-4 03:31
You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant!   Severe rain and snow in the Northwest
seo Email
2022-1-8 15:37
Please continue this great work and I look forward to more of your awesome blog posts.  หวยยี่กี
PG Email Homepage
2022-1-21 15:07
PG   online slot games The most popular of the year 2021, joker game, deposit-withdraw, no minimum, there is a popular game like roma slot, apply for a joker game today, get a 100% bonus if you ask about the hot online gambling game overtaking the curve.
joker game Email Homepage
2022-1-21 15:08
betflix   The latest with a game system designed for direct pg slot players because the game format is very advanced, can play pg slot auto via ios and android systems, supports playing pg slots via mobile Deposit-withdraw automatically in just 8 seconds
Betflix Email Homepage
2022-1-21 15:08
joker game   Including web slots and online casinos With a game format that is easy to play and modern, easy to deposit, withdraw at betflix24 with Betflix auto system, automatic deposit and withdrawal in just 10 seconds. There is an admin service 24 hours a day, no need to make a turn.
เล่นสล็อต Email Homepage
2022-1-21 15:08
เล่นสล็อต  Free Trial PG and Joker is to play slots for free without having to pay a deposit first. There are currently playing slots. mostly through the mobile internet without having to go to the casino to play slots
โรม่า Email Homepage
2022-1-21 15:09
โรม่า  Free Trial PG and Joker is to play slots for free without having to pay a deposit first. There are currently playing slots. mostly through the mobile internet without having to go to the casino to play slots
PGGAMESLOT Email Homepage
2022-1-21 21:24
โรม่า   Roma, the most popular online slots game of all time, roma slot 888, play free, online roma slot games. playing for real money Legendary online gambling game 2021 Roma Slots from famous gaming companies
PGGAMESLOT Email Homepage
2022-1-21 21:24
pg  The latest with a game system designed for direct pg slot players because the game format is very advanced, can play pg slot auto via ios and android systems, supports playing pg slots via mobile Deposit-withdraw automatically in just 8 seconds
PGGAMESLOT Email Homepage
2022-1-21 21:24
ฝาก50รับ150   No need to turn or call each other easily understood is a promotion, deposit 50, get 150 wallet, is another promotion that many people like and think that it is a worthwhile promotion.
PGGAMESLOT Email Homepage
2022-1-21 21:24
เครดิตฟรี  No deposit required is something that online gambling sites. free credit slots pg There is a reward for the members of the web. free credit slots no deposit will be able to play free slots all within the web
分页: 4/15 第一页 上页 1 2 3 4 5 6 7 8 9 10 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]