[文章作者:张宴 本文版本: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) | 阅读(95746)
Hibbah Email
2022-3-4 14:21
Singapore No.1 T-Shirt Printing Solutions | Custom T-shirt Printing | Luminous Printing. Top1 Cheapest & High- Quality T-shirt printing in Singapore? We offer the Stylish & Fastest t-shirt, custom, and commercial printing result in Singapore, Browse our rearmost registers with no-minimum order-fast reversal & Eco friendly! Order Now!  Cheap t shirt printing
Hibbah Email
2022-3-8 15:58
This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.  aksesoris india
Hibbah Email
2022-3-8 22:42
search engine optimization offerings for customers' commercial enterprise web sites to rank first on Google for humans to locate and locate commercial enterprise web sites which can be less difficult to do. with low-cost carrier expenses Confident in getting your customers' commercial enterprise web sites to rank at the website.  รับทำ SEO
Johan Email
2022-3-9 15:14
jasatugaskedokteran merupakan konsultan ilmiah khusus kedokteran dan kesehatan yang sudah berdiri sejak 2015. Sudah banyak lulusan yang kami bantu untuk menyelesaikan tugasnya.Kami melayani :jasa skripsi kedokteranjasa tesis kedokteranskripsi kedokteran uiskripsi kedokteranjudul skripsi kedokteran
Johan Email
2022-3-9 22:47
Dual function wireless charging pad and portable power bank with Type C Input/OutPut. Magsafe Compatible compact power bank.  wireless charging power bank
Hibbah Email
2022-3-13 23:02
Efficiently written information. It will be profitable to anybody who utilizes it, counting me. Keep up the good work. For certain I will review out more posts day in and day out.  Piascledine 300 Osteoarthritis
seo Email
2022-3-17 12:21
Awesome blog. I enjoyed reading your articles.   MUSIC AD AGENCIES
Johana Email
2022-3-19 22:50
Thank you for very usefull information..  harisewell.com
PG Email Homepage
2022-3-22 12:43
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-3-22 12:43
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-3-22 12:43
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-3-22 12:44
เล่นสล็อต  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-3-22 12:44
โรม่า  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
Hibbah Email
2022-3-30 22:24
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.  it rekrutacje
Hibbah Email
2022-4-1 19:26
Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information..  ilovedrawings.com
Hibbah Email
2022-4-2 09:35
Friend, this web site might be fabolous, i just like it.  SOCIAL MEDIA MANAGEMENT MANCHESTER
seo Email
2022-4-3 06:01
i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. المؤسس عثمان 89
Hibbah Email
2022-4-3 13:19
The corporation changed into based via way of means of Mr. Kavin Pongphandecha. as Chief Executive Officer And it changed into registered ultimate year. In addition, rapidly after growing this net platform, it acquired a license from the Ministry of Finance as a internet site this is financially strong and secure for investors.  สมัคร Bitazza
Hibbah Email
2022-4-4 23:07
Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..   recarga jogo
Hibbah Email
2022-4-6 10:21
online slots are prepared for all of us to open up a brand new enjoy in gambling slots XO on line, which has greater than a hundred video games to pick from and each recreation has a jackpot on the way to win, that's greater than 1 million baht.  สมัคร slotxo
分页: 6/15 第一页 上页 1 2 3 4 5 6 7 8 9 10 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]