同时运行PHP脚本 [英] Running concurrent PHP scripts

查看:193
本文介绍了同时运行PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与我的媒体庙以下问题的专用虚拟服务器(默认设置Plesk)。

I'm having the following problem with my media temple dedicated-virtual server(default Plesk setup).

我有一个大的发送文件到浏览器的一个长期运行的PHP脚本。它确实是这样的:

I have a long-running PHP script that sends big files to the browser. It does something like this:

<?php
header("Content-type: application/octet-stream");
readfile("really-big-file.zip");
exit();
?>

此基本上读取来自服务器的文件系统中的文件,并将其发送给浏览器。我不能只用直接链接(让Apache的服务文件),因为业务逻辑在需要应用程序。

This basically reads the file from the server's file system and sends it to the browser. I can't just use direct links(and let Apache serve the file) because there is business logic in the application that needs to be applied.

的问题是,虽然这样的下载正在运行的网站根本不给其他请求。这是严重的问题,特别是对大文件的。

The problem is that while such download is running the site simply doesn't respond to other requests. This is serious issue especially for big files.

在这一点上我不知道这是否是网络,Apache或PHP的问题。

At this point I'm not sure if this is network, Apache or PHP issue.

任何帮助将大大AP preciated。

Any help will be greatly appreciated.

推荐答案

在遇到涉及到的事实,你正在使用的会话的问题。当一个脚本有一个正在运行的会话,它锁定了会议文件,以prevent并发写入这可能会损坏会话数据。这意味着,从同一客户端的多个请求 - 使用相同的会话ID。 - 将不被同时执行,他们将被排队,并只能在一个时间执行一个

The problem you are experiencing is related to the fact that you are using sessions. When a script has a running session, it locks the session file to prevent concurrent writes which may corrupt the session data. This means that multiple requests from the same client - using the same session ID - will not be executed concurrently, they will be queued and can only execute one at a time.

多的用户将不会遇到这个问题,因为他们将使用不同的会话ID。这并不意味着你没有问题,因为你可以设想要访问的网站,而一个文件被下载,或设置多个文件一次下载。

Multiple users will not experience this issue, as they will use different session IDs. This does not mean that you don't have a problem, because you may conceivably want to access the site whilst a file is downloading, or set multiple files downloading at once.

解决的办法其实很简单:拨打<一个href=\"http://php.net/manual/en/function.session-write-close.php\"><$c$c>session_write_close()你开始输出文件之前。这将关闭会话文件,释放锁,并允许更多的并发请求来执行。

The solution is actually very simple: call session_write_close() before you start to output the file. This will close the session file, release the lock and allow further concurrent requests to execute.

这篇关于同时运行PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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