[文章作者:张宴 本文版本: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) | 阅读(98756)
how to clear cache Homepage
2020-2-19 15:15
Cache is the temporary files on your device, from PC to smartphone, it helps website loads faster, clear cache to free more space on your hard disk with how to clear cache
seo Email
2021-11-14 19:12
You make so many great points here that I read your article a couple of times.  ปั่นสล็อต เว็บไหนดี
kkkaaa Email
2021-11-25 13:32
Dankwood pre-rolled is a kind of marijuana that is often used to repair nervousness.in addition,sorrow because it makes you sense extra blissful and more and more more steady.further,some other method to painting the way it affects an individual.again,it makes you sense extra like a watcher than a maker.  dankwoods
mkhan Email
2021-11-26 13:40
Binance is an unstable currency trading platform that attracts a large number of investors. Moreover, the website is easy to use and the design not too complicated so that new players can come and choose easily.  สมัคร binance
Hibbah Email
2021-11-26 14:57
Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign.    turquli seriali galati
Hibbah Email
2021-11-30 14:43
You will discover maximum Android programs right here. As you will know, there are a giant style of Android programs right here. It’s now no longer simply the everyday programs which can be to be had right here. You gets numerous changed programs right here as well. No remember if you’re a gamer or a image designer, you may discover the excellent adjustments to your favourite software program right here.  acmarket
AMKN Email
2021-11-30 18:46
I love the way you write and share your niche! Very interesting and different! Keep it coming!  acrylic swimming pool cost
Hibbah Email
2021-12-1 21:36
There are a lot of government debt relief programs you can leverage when things get tough. You will find many of them that are still available in 2022 below. Remember that no government debt relief program for credit cards debts.  governments debt relief programs
AMK Email
2021-12-2 16:41
I love the way you write and share your niche! Very interesting and different! Keep it coming!  acrylic swimming pool cost
AMKN Email
2021-12-2 22:51
I visit your blog regularly and recommend it to all of those who wanted to enhance their knowledge with ease. The style of writing is excellent and also the content is top-notch.  Thanks for that shrewdness you provide the readers!  <a href="https://bengalfriendly.com/">bengal cats for sale</a>
Hibbah Email
2021-12-2 22:55
I visit your blog regularly and recommend it to all of those who wanted to enhance their knowledge with ease. The style of writing is excellent and also the content is top-notch.  Thanks for that shrewdness you provide the readers!   bengal cats for sale
HK Email
2021-12-3 19:18
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.  situs slot 4d
Hibbah Email
2021-12-4 16:14
Buy dankwoods. DankWoods are crafted from an all herbal Meduro backwood leaf wrapped round 2 grams of all natural hearthplace bud, with quarter gram of listen crafted from nugs, rolled in Kief with a custom made reusable quartz filter  save now with top rate charges to be had world-wide.  dank wood
Hibbah Email
2021-12-5 22:57
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...   gclub
Hibbah Email
2021-12-8 14:15
Thanks for providing recent updates regarding the concern, I look forward to read more.  موسوعة مقروء
JOjo Email
2021-12-8 16:41
Dankwoods are natural pre rolls marijuana blunts from pinnacle shelf weed buds envelope in a custom reusable filter. However human beings stay curious approximately what are dankwoods weed? But it nonetheless stands out, that dankwoods weed or dankwoods pre rolls all imply the equal thing.   Dankwoods
Hibbah Email
2021-12-9 15:21
Instagram merupakan media sosial yang mengedepankan performa visualisasi.Untuk itu desain feed instagram adalah hal yang sangat penting untuk menarik follower ataupun pengikut anda agar betah di beranda anda.   desain feed instagram
Hibbah Email
2021-12-10 18:34
This prefabricated degree is appropriate for occasion use, modeling degree, audio degree, tune degree, live performance degree, promotional events, inn ceremonial dinner halls, convention rooms, auditoriums, buying malls, etc.  เวทีสำเร็จรูป
Hibbah Email
2021-12-10 19:22
Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.  voyance gratuite téléphone 24h/24
AMK Email
2021-12-13 13:59
What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.  forexsignals.com review
分页: 3/15 第一页 上页 1 2 3 4 5 6 7 8 9 10 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]