对于进程了解甚少,不知道我的做法对不对:
<?
//不限时间
set_time_limit(0);#-------------------------------------------------------------------------------
# 开始
#-------------------------------------------------------------------------------
echo '开始';#-------------------------------------------------------------------------------
# 主要工作
#-------------------------------------------------------------------------------
$timeout = 60;
while ($timeout--) {
sleep(1);
flush();//将当前为止程序的所有输出发送到用户的浏览器
}#-------------------------------------------------------------------------------
# 结束
#-------------------------------------------------------------------------------
echo '结束';
?>
<?
//不限时间
set_time_limit(0);#-------------------------------------------------------------------------------
# 开始
#-------------------------------------------------------------------------------
echo '开始';#-------------------------------------------------------------------------------
# 主要工作
#-------------------------------------------------------------------------------
$timeout = 60;
while ($timeout--) {
sleep(1);
flush();//将当前为止程序的所有输出发送到用户的浏览器
}#-------------------------------------------------------------------------------
# 结束
#-------------------------------------------------------------------------------
echo '结束';
?>
#------------------------------------------------------------------------------- # 开始 #------------------------------------------------------------------------------- echo '开始'; #------------------------------------------------------------------------------- # 主要工作 #------------------------------------------------------------------------------- $timeout = 60; while ($timeout--)
{
sleep(1);
}
#------------------------------------------------------------------------------- # 结束 #-------------------------------------------------------------------------------
/*
输出
*/
ob_end_flush();echo '结束';
?>
ob_start();
#-------------------------------------------------------------------------------
# 开始
#-------------------------------------------------------------------------------
echo '开始';#-------------------------------------------------------------------------------
# 主要工作
#-------------------------------------------------------------------------------
$timeout = 60;
while ($timeout--)
{
sleep(1);
}#-------------------------------------------------------------------------------
# 结束
#-------------------------------------------------------------------------------/*
输出
*/
ob_end_flush();
echo '结束';程序的运行,肯定是要分先后的!
即使在这 60 秒有一千个请求 ~ 都不会执行 [主要工作]
直至上一个 [主要工作] 完成才能重新开始再补充一点:
需要在 windows 及 linux 都能用***
文件锁,是一个方法但效能较低
内存打开比文件锁稳定和快速得多.再说win下的文件锁并不可靠.
这是较便宜和快捷的方式.
windows 没有 semaphores
<?php
// Create 100 byte shared memory block with system id if 0xff3
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if (!$shm_id) {
echo "Couldn't create shared memory segment\n";
}// Get shared memory block's size
$shm_size = shmop_size($shm_id);
echo "SHM Block Size: " . $shm_size . " has been created.\n";// Lets write a test string into shared memory
$shm_bytes_written = shmop_write($shm_id, "my shared memory block", 0);
if ($shm_bytes_written != strlen("my shared memory block")) {
echo "Couldn't write the entire length of data\n";
}// Now lets read the string back
$my_string = shmop_read($shm_id, 0, $shm_size);
if (!$my_string) {
echo "Couldn't read from shared memory block\n";
}
echo "The data inside shared memory was: " . $my_string . "\n";//Now lets delete the block and close the shared memory segment
if (!shmop_delete($shm_id)) {
echo "Couldn't shared memory block for deletion.";
}
shmop_close($shm_id);
?> win 2000 好像不支持
楼主的要求只可能在服务器层面才可能完成,php是无能为力的。
当然,若web服务器是由php书写时,那也是可以做到的 任何http请求都将被传递到web服务器(比如apache),并将其传递给php。此时新的php进程就产生了,虽然你可以通过某种手段阻止新进程的运行,但这只是模拟了楼主的要求
windows : =_=
各位~多提一些不同方法~有交流才有进步
思路的核心在于 锁,我们需要一个 锁 来告诉其它进程这一段需要跳过.
至于具体是什么样的锁,可以有不同的实现.
win2k以上的系统,unix,linux都可以使用共享内存来设置这个锁标志.
即使没有共享内存,至少可以生成一个文件来做lock,文件存在即代表有程序在用,不存在则无锁.这在早期PHP文本型论坛中经常用到.
至于更多的方式.甚至数据库.memcached都可以.
甚至CLI命令模式下长驻一PHP程序专门用一个变量来代表锁标志,其它通过web来的PHP进程通过socket或者soap方式来设置/开启锁.都是能变通的办法.
里的ApplicationContext
http://www.leosingleton.com/projects/code/phpapp/
是个文件实现的例子.
其一:就是在服务器端,哪个进程先执行 A 部分的话 哪个进程就生成一个
A.txt的文本在服务器上,其它进程如果想执行A部分的话,判断一个A.txt是否存在,
但这个方法有不好的方面,例如,如果在一个进程刚要生成 A.txt的时候,此时另外一个进程正好判断 A.txt是否存在,
判断结果不存在就执行了 A 部分,但刚才那个进程也会执行 A部分 当然这样的概率很少,其二:就是共享内存当中搞了,不过不兼容linux和window
其三:就是请楼主帮忙看一下我的问题,谢谢了
http://topic.csdn.net/u/20090407/23/b1aede83-d73f-4dd3-b130-4ad9c7b2b650.html
运行的线程在运行前后更新这个变量值。A.php在执行之前,都去看一下变量,如果运行中,就不运行。
Firefox 会先完成一个才再开始下一个
即打开3页要 30 秒, 而不是 10~ 11 秒
不知PHP中有没有这样的函数可调用.....
<?php
ignore_user_abort(true);
$file_path = "lock.php";
$key_a = 'key_a';
$key_b = 'key_b';
$timeout = 10;ob_start();
debugPrint("page start");debugPrint("check token file");
if (file_exists($file_path)) {
debugPrint("the token file exists");
} else {
debugPrint("the token file does not exist, gen key");
$key_a = md5(uniqid(rand(), true));
debugPrint("create token file");
file_put_contents($file_path,"<?php return '$key_a'; ?>");// sleep(5); debugPrint("read token file and get key");
$key_b = @ include($file_path);
}
debugPrint("compare key A & B");
if ($key_a == $key_b) {
debugPrint("key valid");
file_put_contents('log.txt',debugPrint('main start, key = '
.var_export($key_b,true),true),FILE_APPEND);
debugPrint("main program start"); while ($timeout--) {
debugPrint("countdown $timeout s");
sleep(1);
}
debugPrint("main program end");
debugPrint("delete token file");
unlink($file_path);
} else {
debugPrint("the keys mismatch");
}debugPrint("page end");
ob_end_flush();function debugPrint($msg,$return = false) {
if (true === $return) {
return '['.date('Y-m-d H:i:s').'] '.$msg."\n";
} else {
echo '['.date('Y-m-d H:i:s').'] '.$msg."<br />\n";
ob_flush();
flush();
return true;
}
}
?>只要原理正确,可以改成 shared memory
请各位测试看看有没有问题?
我...开会去也 =_=