PHP脚本同步
我需要在后台启动一些 php 脚本(无需等待),然后等待所有进程终止,类似于屏障的东西(如果您熟悉多线程)。
示例:
//...code...
run_script('myscript1.php');//it's like an exec but doesen't wait for the script to finish
run_script('myscript2.php');//it's like an exec but doesen't wait for the script to finish
run_script('myscript3.php');//it's like an exec but doesen't wait for the script to finish
do_something();
wait_until_all_proc_are_finished();//it will wait until all script are executed
do_something_else();
//....
我创建了一个应该可以解决问题的脚本;我在控制台中测试了它,它工作得很好,但它在 php 页面上不起作用,我不明白为什么!
class TSync{
private $threads=array();
function tcreate($p){
$tname=tempnam(null,'THS_');//create a temp name
$p=addslashes($p);//just to be sure
$name=addslashes($tname);
$ex= 'php -r "$fp=fopen(\''.$name.'\',\'r+\');flock($fp,LOCK_EX);include(\''.$p.'\');fclose($fp);"';
run_on_background($ex);//execute it on background
$this->threads[count($this->threads)]=$tname;
return count($this->threads)-1;//returns the thread "id"
}
function twait($id){
$f=$this->threads[$id];//recover the name
/***even this doesen't work***
$fp=fopen($f,'r');
flock($fp,LOCK_EX);
fclose($fp);
*/
echo date("H:i:s"),"#Locking on $f<br/>";
$ex= 'php -r "$fp=fopen(\''.$f.'\',\'r\');flock($fp,LOCK_EX);fclose($fp);"';
exec($ex);
unlink($f);
}
}
$t=new TSync();//create the class
$f=$t->tcreate(dirname(__FILE__).'/testth.php');//this is a test script that waits 10s
$t->twait($f);//now you should wait the script,commenting this line should result on the script not waiting
在控制台上启动的有效代码的示例(在 Windows 上测试)
start /b php.exe -r "$fp=fopen('C:\\Windows\\Temp\\THS6D7.tmp','w');flock($fp,LOCK_EX);include('C:/.../testth.php');fclose($fp);"
如果我多次启动代码,第二个脚本将等待第一个脚本,因此它应该可以工作。
I need to launch some php script in background (without waiting)and then wait until all processes are terminated,something(if you are familiar with multithreading)similar to a barrier.
Example:
//...code...
run_script('myscript1.php');//it's like an exec but doesen't wait for the script to finish
run_script('myscript2.php');//it's like an exec but doesen't wait for the script to finish
run_script('myscript3.php');//it's like an exec but doesen't wait for the script to finish
do_something();
wait_until_all_proc_are_finished();//it will wait until all script are executed
do_something_else();
//....
I created a script that should do the trick;i tested it in console and it works nice but it doesen't work on php pages,i don't understand why!
class TSync{
private $threads=array();
function tcreate($p){
$tname=tempnam(null,'THS_');//create a temp name
$p=addslashes($p);//just to be sure
$name=addslashes($tname);
$ex= 'php -r "$fp=fopen(\''.$name.'\',\'r+\');flock($fp,LOCK_EX);include(\''.$p.'\');fclose($fp);"';
run_on_background($ex);//execute it on background
$this->threads[count($this->threads)]=$tname;
return count($this->threads)-1;//returns the thread "id"
}
function twait($id){
$f=$this->threads[$id];//recover the name
/***even this doesen't work***
$fp=fopen($f,'r');
flock($fp,LOCK_EX);
fclose($fp);
*/
echo date("H:i:s"),"#Locking on $f<br/>";
$ex= 'php -r "$fp=fopen(\''.$f.'\',\'r\');flock($fp,LOCK_EX);fclose($fp);"';
exec($ex);
unlink($f);
}
}
$t=new TSync();//create the class
$f=$t->tcreate(dirname(__FILE__).'/testth.php');//this is a test script that waits 10s
$t->twait($f);//now you should wait the script,commenting this line should result on the script not waiting
An example of the effective code launched on console (tested on windows)
start /b php.exe -r "$fp=fopen('C:\\Windows\\Temp\\THS6D7.tmp','w');flock($fp,LOCK_EX);include('C:/.../testth.php');fclose($fp);"
If i launch the code multiple times the second script will wait the first so it should work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够使用 Gearman 解决此问题
You should be able to solve this by using Gearman