[文章作者:张宴 本文版本:v1.0 最后修改:2007.11.16 转载请注明出处:http://blog.zyan.cc]
最近遇到一个问题,Linux下的PHP命令行程序作为守护进程,需要从队列文件中读一行数据,通过TCP协议发送给外地的接收服务器,再读下一行数据,再发送。当本地与外地的网络状况不好时,有时候发送一条数据所耗费的时间就较长,累积起来容易造成队列堵塞和延迟。
于是,我准备用该PHP命令行程序生成多个子进程,将串行处理变成并行处理。最简单的方法就是在PHP中用exec()或popen()函数将一个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因PHP解析器所在的路径)
查看/opt/zhangyan.sh打下的日志文件的第一行和最后一行:
可以看出,500进程并发处理这1000条数据只耗费5秒钟。而按照原来的串行模式,处理每条数据即使只耗费最短的1秒钟,也需要1000秒,约合16分钟才能完成。
PS:将PHP程序作为Linux守护进程的方法:
(nohup命令可以在用户退出终端后仍然执行程序,“2>&1 > /dev/null”表示不显示标准输出和错误输出,最后的&表示推到后台执行。)
最近遇到一个问题,Linux下的PHP命令行程序作为守护进程,需要从队列文件中读一行数据,通过TCP协议发送给外地的接收服务器,再读下一行数据,再发送。当本地与外地的网络状况不好时,有时候发送一条数据所耗费的时间就较长,累积起来容易造成队列堵塞和延迟。
于是,我准备用该PHP命令行程序生成多个子进程,将串行处理变成并行处理。最简单的方法就是在PHP中用exec()或popen()函数将一个shell命令行推到后台去执行,例如:
<?php
exec("/bin/sh /opt/zhangyan.sh &");
?>
最后的&表示将shell脚本推到后台去执行。exec("/bin/sh /opt/zhangyan.sh &");
?>
但是这样会有一个问题,如果推到后台的进程太多,可能会导致服务器系统资源耗尽而崩溃,所以必须控制进程数量。
我写了一个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.cctail -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”表示不显示标准输出和错误输出,最后的&表示推到后台执行。)
PGGAMESLOT
2022-1-21 21:24
joker game Direct web slots, not through agents online gambling games The most legendary casino game Stuck in one of the 5 most popular slot game camps. With more than 150 games to choose from, Joker Slots
PGGAMESLOT
2022-1-21 21:24
โจ๊กเกอร์ Direct web slots, not through agents online gambling games The most legendary casino game Stuck in one of the 5 most popular slot game camps. With more than 150 games to choose from, Joker Slots
PGGAMESLOT
2022-1-21 21:24
ฝาก20รับ100 A promotion that many people like and think that it is a worthwhile promotion.
PGGAMESLOT
2022-1-21 21:25
19รับ100 Latest 2021 Promotion Hits Slots Can play all game camps, new members, deposit 30, get 100, give away free credit, no need to deposit, no need to share Promotion deposit 30 get 100 unlimited withdrawal No minimum deposit
PGGAMESLOT
2022-1-21 21:25
Slot 888 The number 1 online slot game in Thailand, slot 888 online that includes the 888 slot game camp to play more than 300 games.
PGGAMESLOT
2022-1-21 21:25
สล็อต 888 The number 1 online slot game in Thailand, slot 888 online that includes the 888 slot game camp to play more than 300 games.
PGGAMESLOT
2022-1-21 21:25
ทดลองเล่นสล็อต Slots Free Trial Playable Withdrawable Free Trial Playable Withdraw Real Money Free credit to play slots Terms and conditions are as specified by the website.
saladar
2022-1-28 23:13
Top sites that you should not miss. A website that will make you rich by playing slots Don't wait, come in.เครดิตฟรี
Hibbah
2022-2-9 13:43
Public debt relief has been around for further than a decade and has helped a lot of debtors to pay off their debts in lower quantities than they owe. You don't have anything to deposit before the process starts with the debt operation plan they give you. Government Debt Relief Programs
JAKON
2022-2-18 13:56
Indeed disability benefits, similar as disability withdrawal, can be impacted according to the person's donation time. como aumentar o valor da aposentadoria
seo
2022-2-24 16:22
Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home. รับรูดบัตรเครดิตเป็นเงินสดออนไลน์
Hibbah
2022-2-25 01:26
I have read your article couple of times because your views are on my own for the most part. It is great content for every reader. youlikebet
seo
2022-2-26 22:30
Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home. รับรูดบัตรเครดิตเป็นเงินสดออนไลน์
PG
2022-2-27 14:29
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
2022-2-27 14:29
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
2022-2-27 14:30
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.
โรม่า
2022-2-27 14:30
โรม่า 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
เครดิตฟรี
2022-2-27 14:30
เครดิตฟรี Latest free credit 2021 online gambling website latest free credit All in one website, free credit, PG, slot promotions. Free credit, no deposit, no sharing
Hibbah
2022-2-27 19:34
กำจัดปลวกลำพูน ( lamphun termite control ) เป็นบริษัทกำจัดปลวกลำพูน ฉีดปลวกลำพูนราคาถูก เน้นความคุ้มค่า เพื่อลดค่าใช้จ่ายของลูกค้า กําจัดปลวกลำพูนราคา บริษัทกําจัดปลวกลำพูนราคา ฉีดปลวกลำพูนราคา กําจัดปลวกลำพูนราคา เริ่มต้นเพียง 3,500 บาท โปรโมชั่นสำหรับลูกค้าที่ฉีดเพื่อป้องกันปลวกประจำปี ตามขนาดพื้นที่ของบ้าน .โดยอัดน้ำยาฉีดปลวกใต้ดิน ฉีดปลวกทั้งภายใน และภายนอกบ้าน วางเหยื่อล่อปลวก และ วางท่อปลวก พร้อมทั้งให้คำปรึกษาตามความต้องการของลูกค้าได้ทุกรูปแบบ. ประเมินราคาฟรี ไม่มีค่าใช้จ่ายใดๆ รับกำจัดปลวกลำพูน อำเภอเมือง , กำจัดปลวกป่าซาง ลำพูน , กำจัดปลวกบ้านโฮ่ง ลำพูน , กำจัดปลวกแม่ทา ลำพูน , กำจัดปลวกลี้ ลำพูน , กำจัดปลวกลี้ ลำพูน , กำจัดปลวกลี้ ลำพูน lamphun termite control
AKO
2022-2-28 00:37
it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity.. ufabet
分页: 5/15 1 2 3 4 5 6 7 8 9 10