[文章作者:张宴 本文版本: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 | 评论(269) | 引用(4) | 阅读(93900)
shzz Email
2022-7-2 16:41
I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.  Lead generation
shz Email
2022-7-2 16:42
I was just browsing through the internet looking for some information and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject. Bookmarked this page, will come back for more.  Lead generation
shzz Email
2022-7-2 16:45
This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information...  Lead generation
shz Email
2022-7-2 16:46
It is perfect time to make some plans for the future and it is time to be happy. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!  Lead generation
shz Email
2022-7-2 16:49
Two full thumbs up for this magneficent article of yours. I've really enjoyed reading this article today and I think this might be one of the best article that I've read yet. Please, keep this work going on in the same quality.  Lead generation
shzz Email
2022-7-2 16:50
I surely acquiring more difficulties from each surprisingly more little bit of it  Lead generation
shz Email
2022-7-2 16:52
Cool stuff you have and you keep overhaul every one of us  Lead generation
shzz Email
2022-7-2 16:55
As a seller of legal steroids, you can buy Crazy Bulk products, explore stacks and finally get the body you’ve always wanted  Lead generation
shz Email
2022-7-2 16:56
Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers.  Lead generation
shzz Email
2022-7-2 16:58
I read your post and I found it amazing! thank!  Lead generation
seo Email
2022-7-15 17:15
I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work...  g2g สล็อต
Hibbah Email
2022-7-26 13:19
Keep up the good  work , I read few posts  on this  web site  and I  conceive that your blog  is very   interesting and has   sets  of  fantastic   information.  Gaskeunbet
Hibbah Email
2022-7-28 11:04
What is an outstanding post! “I’ll be back” (to read more of your content). Thanks for the nudge!  bluetooth speaker supplier
Hibbah Email
2022-7-30 11:03
All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks  foldable bike for adults
seo Email
2022-7-30 20:26
Thank you because you have been willing to share information with us.   dmt vape pen
Jojokhatri Email
2022-7-31 10:59
Thank you for sharing a bunch of this quality contents, I have bookmarked your blog. Please also explore advice from my site. I will be back for more quality contents.  cetak buku bandung
Akon Email
2022-8-3 15:59
Chime may want to be a economic technological know-how company that was once formed on the idea that primary banking merchandise ought to be helpful, simple, and free. invoice of alternate fees, month-to-month provider prices, carrier fees, minimal stability restrictions, and choice costs do not appear to be employed in the Chime model.  chime routing number
Dada Email
2022-8-3 21:39
We area unit one among the leading road signs makers firms in African country. Our presence is felt so much and wide across east & Central Africa. We make, stock and sell a large form of Kenyan normal road signs, traffic signs & street signs as well as stop.  road signs manufactures companies in kenya
seo Email
2022-8-4 16:15
Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors.  http://159.65.15.93/
FDSSW Email
2022-8-4 22:08
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.  consumer feedback
分页: 9/14 第一页 上页 4 5 6 7 8 9 10 11 12 13 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]