PHP-fread()等到另一个fwrite()脚本结束 [英] PHP - fread() waiting until another fwrite() script ends

查看:115
本文介绍了PHP-fread()等到另一个fwrite()脚本结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

this.lau_ 所述,似乎问题在于服务器上根本没有运行任何代码当第一个文件正在运行时.我猜有些配置只能一次运行一个脚本-每个用户"只能运行一个脚本.

As this.lau_ commented, it seems the problem is that no code is running at all on the server when the first file is running. I guess some configuration only allows one script to be run at once - or only one script per "user".

现在,我将调查是什么导致服务器上的该行为.谢谢大家的帮助!

Now I will investigate what can be causing that behaviour on the server. Thank you everyone for your help!

.

我在单独的文件上有两个PHP脚本:

I've got two PHP scripts, on separate files:

/* The first file contains */
for($i = 0; $i < 10000; $i++){
    $w = fopen($progress_file, "w");
    fwrite($w, $i);
    fclose($w);
}

/* The second file contains */
$f = fopen($progress_file, "r");
$progress_read = fread($f, filesize($progress_file));
fclose($f);
echo $progress_read !== false ? $progress_read : 0;

如您所见,我做了一个循环,所以我有时间刷新第二个文件,并等待它提供当下写入的数字.

As you can see, I make a loop so I have time to refresh second file and wait for it to give the the number it's written in that moment.

我知道,在执行fopen($file, "w");时,我正在截断该文件,因此它(有时)不会给我应该提供的数字,而它会给出0.但是,它应该回显某些内容,因为我检查是否可以读取文件,否则回显"0".

I know that, when doing fopen($file, "w"); I'm truncating that file, so it won't give me (sometimes) the number it should give, and it will give 0. However, it should echo something, as I check if the file has been able to be read and, otherwise, echo "0".

相反,我让第二个文件等待(显示为正在加载)而没有回声,直到第一个文件完成整个循环为止(因此,我总是在第二个文件上以"9999"结尾).

Instead, I get the second file waiting (shown as loading) without echoing nothing until the first file gets completed the whole loop (so I always end up with "9999" on the second file).

我已经尝试过更改fopen()的模式,但是没有运气,并且将fopen()fclose()置于循环之外.也没有运气.我还尝试使用file_get_contents()和其他功能更改fopen-fread-fclose东西,但没有任何效果.

I've tried changing the modes of fopen() with no luck, as well as making the fopen() and fclose() outside the loop. No luck neither. I've also tried changing the fopen-fread-fclose thing with file_get_contents() and other functions and nothing has worked.

fflush()ob_flush()flush()也没有任何区别.

Also, fflush(), ob_flush() and flush() haven't made any difference.

正如我所说,文件已写入(如果我使用文本编辑器打开文件,则看到内容更改),即使没有,它也应回显"0".

As I said, the file gets written (if I open it with a text editor, I see the content changes) and even if it wouldn't, it should echo "0".

为什么第二个文件在第一个循环结束之前一直等待而不回声?

谢谢.

推荐答案

第二个脚本不等待.您要么需要使用

The second script does not wait. You either need to use

file_put_contents($filename,$str,LOCK_EX);

强制文件等待,或者您需要延迟执行一些循环.

to force files waiting or you need to implement some loop with delay.

您可以在脚本的开头添加一个循环,以同时开始测试,如下所示:

You could add a loop on the begin of your script to start test at same time like this:

echo "flock test ".time()."<br>";
$constant_time = 1570612500;
while ( time()<$constant_time )
{
usleep(500);
}

$ constant_time是您希望同时启动两个脚本的时间.给它以echo + 20秒显示的当前时间,然后将其上传到服务器上,然后运行脚本(您需要在时间过去之前执行此操作.这样,两个脚本将同时启动.

Where $constant_time is time when you wish to start both scripts in the same time. Give it current time shown by echo + 20 seconds, then upload it on server, then run the scripts (you need to do that before the time elapsed. This way both scripts will start at the same time.

PS:我最初搜索的答案是,在设置LOC_EX并且出现此问题时,fread是否可以等待.

PS: I originally searched answer if fread can wait when LOC_EX is set and this question came out.

这篇关于PHP-fread()等到另一个fwrite()脚本结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆