Apache + PHP 同时运行多个脚本 [英] Apache + PHP multiple scripts at the same time

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

问题描述

美好的一天.

首先,抱歉我的英语不好=)

For first, sorry for my bad English =)

所以.我创建了脚本:

<?
sleep(10);
?>

我的 Apache 有 MPM 模块,我显然没有在这个脚本中使用会话,只是……只是 sleep(10).当我在浏览器中同时打开 2 个标签时,第一个标签在 10 秒内加载,第二个标签 - 20 秒.

My Apache has MPM module, I obviously didn't use sessions in this script, just.. just sleep(10). When I open 2 tabs in my browser simultaneously, first tab loads in 10 seconds, second tab - 20 seconds.

但是.当我同时在 2 个不同的浏览器中打开此脚本时,它会在 10 秒后在每个浏览器中加载.

But. When I open this script in 2 different browsers at the same time, it loads in each one after 10 seconds.

所以,我开始思考,我的问题是连接:保持活动".我改变了我的脚本:

So, I started thinking, that my problem is "Connection: Keep-Alive". I changed my script:

<?
  header('Connection: close');
  phpinfo();
  sleep(10);
?>

phpinfo() - 可以肯定的是,标头是在 sleep() 之前发送的.Buuuut...我遇到了同样的问题.在 Chrome 的第一个选项卡中,我收到带有Connection: close"的标题,在第二个选项卡中,我无法在第一个脚本未结束时获取响应标题.在两个不同的浏览器中 - 一切正常.

phpinfo() - to be sure, that headers were sent before sleep(). Buuuut... I meet the same problem. In first tab of Chrome I get headers with "Connection: close", in second tab I can't get response headers while first script is not ended. In two different browsers - everything is normal.

现在我完全不知道我做错了什么.为什么 Chrome 无法对我的网站进行 2 个并行查询?我应该怎么做才能解决这个问题?

And now I have absolutely no ideas what I'm doing wrong. Why Chrome can't make 2 parallel queries to my site? What I should do to solve this problem?

附言我不想为我的所有网站禁用 keep-alive.我不介意,它是否会加快 css、图像和其他东西的加载速度.甚至其他脚本.但我希望能够在一个浏览器中并行运行一些脚本.P.P.S.例如:在一个页面上将会有很长的ajax查询,例如 - 在服务器端处理一些大数据和一些很小的间隔的ajax查询 - 以获取执行第一个查询的状态.显然,它们必须是平行的.

P.S. I don't want to disable keep-alive for all my site. I don't mind, if it will speed up loading of css, images and other stuff. Even other scripts. But I want to have ability to run some scripts parallel in one browser. P.P.S. For example: at the one page will be very long ajax query, for example - processing some big data at server-side and ajax queries with some little interval - to get status of executing first query. Obviously, that they must be parallel.

推荐答案

我知道这是一个老问题,但我刚刚遇到了同样的问题并使用 session_write_close()!如果没有它,PHP 会故意为同一会话排队脚本.

I know it's an old question but I just had the same problem and solved it with session_write_close()! Without it PHP purposely queues scripts for same session.

最简单的例子:

Looong 脚本 #1:

<?php

$_SESSION['progress'] = 0;

for ($i=0; $i < 100; $i++)
{
    session_start();
    $_SESSION['progress']++;
    session_write_close();
    sleep(1);// This is slowing script purposely!
}

?>

短脚本 #2:

<?php
session_start();
print_r($_SESSION['progress']);
?>

现在尝试一下,打开第一个需要很长时间的脚本在新选项卡中打开第二个脚本,并在第一次仍在运行时眨眼更新进度!这么容易吧?!;)

Now try it, open first script that takes ages open second script in new tab and get the progress updated in a blink while first still running!! So easy right?! ;)

ajax轮询长脚本和第二次ajax调用获取进度的原理相同!

Same principle for ajax polling long script and second ajax call to get the progress!

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

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