[文章作者:张宴 本文版本: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) | 阅读(98750)
Hibbah Email
2022-8-6 16:03
Veger offer a range of high tech fast charging solutions for everyone including laptop power banks, magsafe power banks, charging cables and gan chargers with PD/QC 3.0 tech. Check out their website now to know more about their products.  Power Bank
Hibbah Email
2022-8-7 16:45
Bermain judi slot on-line merupakan kegiatan baru principle sangat populer di masyarakat Dutch East Indies saat ini. Game slot on-line sudah mempunyai penggemar tersendiri di masyarakat Dutch East Indies. Dengan terus bermunculannya situs judi slot on-line terbaru, ligasloto hadir untuk shisha principle ingin bermain judi slot on-line uang asli.  judi slot online
seo Email
2022-8-7 18:11
Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.  Who can be in the Abbott Baby Formula Recall Lawsuit?
seo Email
2022-8-10 14:33
This is my first time i visit here. 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 workYes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more.  cbd lille
seo Email
2022-8-11 16:21
I learn some new stuff from it too, thanks for sharing your information.  transkrypcje wywiadow ssi
HBGUS Email
2022-8-12 13:21
I got what you mean  , thanks  for posting .Woh I am happy  to find this website through google.  No Credit Check Loans
seo Email
2022-8-12 14:32
You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...  singapore best water purifier
seo Email
2022-8-12 14:37
Admiring the time and effort you put into your blog and detailed information you offer!..  Situs Slot Online Tergacor
seo Email
2022-8-12 17:47
I learn some new stuff from it too, thanks for sharing your information.  road signs in kenya and their meaning
Yumka Email
2022-8-15 21:54
Remarkable article, it is particularly useful! I quietly began in this, and I'm becoming more acquainted with it better! Delights, keep doing more and extra impressive!  sekolah pelatihan kapal pesiar
Sadag Email
2022-8-19 23:12
This subject matter is coded by, zoutula. Enfant college and kindergarten wordpress subject matter is an excellent clean, plugin rich, customizable, rapid loading and properly designed day care subject matter.  wordpress themes for day care
seo Email
2022-8-23 22:04
I suppose I'm lucky to have found this! I hope you will be adding more in the future...   สมัครแทงบอล
Hibbah Email
2022-8-25 23:23
It wishes to be repaired as quickly as possible, or meals and different matters may also rot. In one of these situation, it's miles crucial to rent one of the expert fridge restore offerings for solving the problem.  Assistência Técnica de Geladeira
seo Email
2022-8-30 16:50
This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform!  ambbet เว็บตรง
seo Email
2022-9-9 03:17
A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post.  satta
seo Email
2022-9-10 03:14
Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback.  The iCon System
seo Email
2022-9-11 01:20
Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..  ทางเข้า ambbet
seo Email
2022-9-12 17:22
Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.  g2gbet
seo Email
2022-9-13 06:23
Great things you’ve always shared with us. Just keep writing this kind of posts.The time which was wasted in traveling for tuition now it can be used for studies.Thanks  โต๊ะพูล
aree Email Homepage
2022-9-16 17:45
I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.  haus on handy
分页: 10/15 第一页 上页 5 6 7 8 9 10 11 12 13 14 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]