通过AJAX处理大数据集没有带来速度的好处 [英] Processing large data sets via AJAX brings no speed benefits

查看:172
本文介绍了通过AJAX处理大数据集没有带来速度的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个耗时的数据库查询运行。每个已建成可以选自在网页上的一个选项触发。我想我是被通过的若干AJAX请求发射了工作相当狡猾。

余presumed多个请求将被分割在多个进程/也就是说,工作线程将相对快速地完成了用户

然而,的请求似乎在串行下,即没有速度益处由用户感觉到。要被处理

更糟糕的是,AJAX请求更新的页面也排队等候,这意味着他们没有反应,直到previous请求已经全部完成。

予有的是,这可能被PHP引起会议被锁定。

什么是此类问题的通常的做法?

  • 有没有办法强制AJAX请求异步工作?
  • 在我可以阻止锁定会话PHP?
  • 我应该使用一个单独的程序通过cron火后台运作?

谢谢!

NB该项目已使用symfony框架构建。

AJAX使用jQuery的

  //获取内容
$获得('/ AJAX / itemInformation /塞/+塞,功能(数据){
  $('#模式,更多信息)HTML(数据);
});
 

解决方案

如果您在任何给定的AJAX请求使用会话的所有后,他们将有效地串行执行,从而请求。这是由于在会话数据文件在操作系统级锁定。关键要获得这些请求是异步的是尽快关闭(或从未启动)会议成为可能。

您可以使用 session_write_close 文档),以尽快关闭会话。以最快的速度进出越好 - 我喜欢用几个辅助功能对于这一点, set_session_var 功能将在下面打开的会话,写VAR,然后关闭会话。页面加载后,你可以叫在session_start 得到填充 $ _ SESSION 变量,然后立即拨打 session_write_close 。从那时起出来,只用一组函数下面写的。

get函数完全是可选的,因为你可以简单地参照 $ _ SESSION 全球性的,但我喜欢用这个,因为它提供了一个默认值,我可以少了一个三元中的code中的主体。

 函数get_session_var($键=假,$默认= NULL){
    如果($关键==假||的strlen($键)℃下)
        返回false;
    如果(使用isset($ _ SESSION [$关键]))
        $ RET = $ _SESSION [$关键]
    其他
        $ RET = $默认​​;
    返回$沤;
}
功能set_session_var($键=假,$值= NULL){
    如果($关键==假||的strlen($键)℃下)
        返回false;
    在session_start();
    如果($值===空)
        取消设置($ _ SESSION [$关键]);
    其他
        $ _SESSION [$关键] = $价值;
    session_write_close();
}
 

请注意,有一组全新的考虑,一旦AJAX请求是真正同步。现在你要注意竞争条件(你必须要警惕的一个请求设置可能影响另一个请求的变量) - 你看,随着会议结束,一个请求的变更 $ _ SESSION 将不可见的另一请求,直到它重建值。你可以帮助避免这种情况,立马一个关键用途之前,重建的 $ _ SESSION 变量:

 函数rebuild_session(){
    在session_start();
    session_write_close();
}
 

...但是这仍然容易受到竞争状态。

I have several time consuming database queries to run. Each has been built to be triggered from an option chosen on a web page. I thought I was being quite cunning by firing off the work via several AJAX requests.

I presumed that multiple requests would be split over multiple processes/threads meaning the work would be completed relatively quickly for the user.

However, the requests seem to be processed in serial, meaning that no speed benefit is felt by the user.

Worse still, AJAX requests to update the page also wait in line, meaning they fail to respond until the previous requests have all completed.

I have read that this may be caused by the PHP sessions being locked.

What is the usual approach for this kind of issue?

  • Is there a way to force AJAX requests to work asynchronously?
  • Can I stop PHP from locking the sessions?
  • Should I use a seperate process via cron to fire background workings?

Thanks!

NB This project has been built using the symfony framework.

AJAX uses jQuery

// Get the content
$.get('/ajax/itemInformation/slug/'+slug, function(data) {
  $('#modal-more-information').html(data);
});

解决方案

If you are using sessions at all during any of the given AJAX requests, they will effectively execute serially, in order of request. This is due to locking of the session data file at the operating system level. The key to getting those requests to be asynchronous is to close (or never start) the session as quickly as possible.

You can use session_write_close (docs) to close the session as soon as possible. I like to use a couple of helper functions for this, the set_session_var function below will open the session, write the var, then close the session - in and out as quickly as possible. When the page loads, you can call session_start to get the $_SESSION variable populated, then immediately call session_write_close. From then on out, only use the set function below to write.

The get function is completely optional, since you could simply refer to the $_SESSION global, but I like to use this because it provides for a default value and I can have one less ternary in the main body of the code.

function get_session_var($key=false, $default=null) {
    if ($key == false || strlen($key) < 0)
        return false;
    if (isset($_SESSION[$key]))
        $ret = $_SESSION[$key];
    else
        $ret = $default;
    return $ret;
}
function set_session_var($key=false, $value=null) {
    if ($key == false || strlen($key) < 0)
        return false;
    session_start();
    if ($value === null)
        unset($_SESSION[$key]);
    else
        $_SESSION[$key] = $value;
    session_write_close();
}

Be aware that there are a whole new set of considerations once the AJAX requests are truly asynchronous. Now you have to watch out for race conditions (you have to be wary of one request setting a variable that can impact another request) - for you see, with the sessions closed, one request's changes to $_SESSION will not be visible to another request until it rebuilds the values. You can help avoid this by "rebuilding" the $_SESSION variable immediately before a critical use:

function rebuild_session() {
    session_start();
    session_write_close();
}

... but this is still susceptible to a race condition.

这篇关于通过AJAX处理大数据集没有带来速度的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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