本文介绍了PHP/Ajax轮询进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对php脚本循环进行实时轮询,到目前为止,我的尝试还没有运气.到目前为止,这是我所拥有的:

I am attempting to implement a live polling of a php script loop with no luck thus far in my attempts. Here is what I have thus far:

在表单上提交:

$.ajax({
data: $(this).serialize(),
success: showResponse,
url: 'process.php',
type: 'post'
});

function showResponse(){
$.ajax({
type: "GET",
url: "progress.php",
cache: false,
success: function(data) {
var response = $.parseJSON(data);
if (response.processing === true) {
console.log("Current Item: " + response.currentItem +
"Total Items: " + response.totalItems +
 "Percent Complete: " + response.percentComplete);
setTimeout(checkProgress, 1000);
});
}

在process.php脚本中:

In the process.php script:

session_start();
echo json_encode(array("processing" => true));
$totalItems = 10000000;
$_SESSION['totalItems'] = $totalItems;
$_SESSION['processing'] = true;
$_SESSION['error'] = false;
for ($i=0; $i <= $totalItems; $i++) {
$_SESSION['currentItem'] = $i;
$_SESSION['percentComplete'] = round(($i / $totalItems * 100));
}

在进度php脚本中:

session_start();
echo json_encode(array(
"processing" => $_SESSION['processing'],
"error" => $_SESSION['error'],
"currentItem" => $_SESSION['currentItem'],
"totalItems" => $_SESSION['totalItems'],
"percentComplete" => $_SESSION['percentComplete']
)
);

不确定我在哪里出错,但是一旦达到100%完成,它所做的就是循环.任何建议将不胜感激!

Not sure where I am going wrong here but all it does is loop once it hits 100% complete. Any suggestions would be greatly appreciated!

编辑我将以上内容更改为在process.php中使用apc:

EDITI changed the above to using apc in the process.php:

apc_store('totalItems', $totalItems);
apc_store('processing', true);
apc_store('error', false);
apc_store('currentItem', $i);
apc_store('percentComplete', round(($i / $totalItems * 100)));

在progress.php中:

And within the progress.php:

echo json_encode(array(
"processing" => apc_fetch('processing'),
"error" => apc_fetch('error'),
"currentItem" => apc_fetch('currentItem'),
"totalItems" => apc_fetch('totalItems'),
"percentComplete" => apc_fetch('percentComplete')
    )
);

仍然无法按照我想要的方式正常工作,我做错了什么吗?它仅显示错误值,直到脚本完成为止,并显示100%,就像会话使用之前一样.有什么想法吗?

Still doesn't work properly the way I am wanting it to work, am I doing something incorrectly? It only shows false values until the script completes and shows 100% just like the session use was doing before. Any ideas?

推荐答案

会话信息是只能用于独占的资源,您没有考虑到这一点.

The session information is a resource that can only be used exclusively, and you have not taken this into account.

具体来说,在默认设置下,session_start使PHP获得对包含会话数据的文件的排他锁.在脚本退出或调用session_write_close之前,该文件不会被解锁.

Specifically, under default settings session_start causes PHP to acquire an exclusive lock on a file that contains the session data. This file is not unlocked until the script exits or session_write_close is called.

在您的示例中,process.php获取该锁并开始工作.同时,progress.php会尝试session_start()而不能(由于锁定). process.php需要花费足够的时间才能完成并退出(因此释放锁),才能满足对进度信息的请求.

In your example, process.php acquires the lock and starts working. In the meantime, progress.php tries to session_start() and cannot (due to the lock). Enough time needs to pass for process.php to complete and exit (thus releasing the lock) before the request for progress information can be satisfied.

您可以进行的一个小更改立即生效,即在您的工作程序循环中调用session_write_closesession_start:

A small change you can make that will have immediate effect is to call session_write_close and session_start from within your worker loop:

for ($i=0; $i <= $totalItems; $i++) {
    session_start();
    $_SESSION['currentItem'] = $i;
    $_SESSION['percentComplete'] = round(($i / $totalItems * 100));
    session_write_close();
}

这将允许两个脚本轮流锁定会话存储文件,因此您将看到一切正常.但是,性能会存储(这是处理会话存储文件的一种不礼貌的方式).

This will allow the two scripts to take turns locking the session storage file, so you will see things working as intended. However, performance will tank (this is a really impolite way to treat the session storage file).

如果您在现实世界中需要做类似的事情,那么有必要利用会话数据以外的东西来实现PHP脚本之间的信息交换(例如,内存中的缓存,例如APC或memcached ).

If you had need to do something like this in the real world, it would be necessary to utilize something other than the session data to enable this exchange of information between the PHP scripts (e.g. an in-memory cache like APC or memcached).

这篇关于PHP/Ajax轮询进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:28
查看更多