在同一时间PHP运行的循环和脚本 [英] PHP run loop and script at same time

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

问题描述

我目前运行在我的PHP脚本一个循环来检查用户是否连接中止(<一href="http://stackoverflow.com/questions/11627916/connection-aborted-does-not-work-on-ajax-calls">connection_aborted()不能在阿贾克斯工作电话):

I'm currently running a loop in my PHP script to check if the user connection has been aborted (connection_aborted() does not work on ajax calls):

connection_check.php

<?php
ignore_user_abort(true);
for ($i = 0; $i < 1000; $i++) {
    echo "<br>";
    flush();
    ob_flush();
    if (connection_aborted()) {
        echo "nocon";
        exit;
    } else {
        // Everything is fine
    }
    sleep(3);
}
?>

不过,我也需要运行我的PHP code中的休息,而这个循环中的背景,这样当连接被中断,脚本死亡运行,但如果不是,该脚本应该继续运行。我需要这样做,因为我调用这个PHP文件与Ajax调用。

However, I also need to run the rest of my PHP code while this loop is running in the "background" so that when the connection is aborted, the script dies, but if it is not, the script should continue to run. I need to do this because I'm calling this PHP file with an ajax call.

这是我尝试过,但是,它不工作(脚本继续运行,并没有死, connection_check.php 不回声NOCON ):

This is what I've tried, however, it isn't working (the script continues to run and does not die, connection_check.php does not echo "nocon"):

为file.php

<?php
$child = fopen('http://ipaddress/core/connection_check.php', 'r');

/***
* ALL PHP CODE HERE THAT SHOULD START RUNNING RIGHT AWAY WHILE CONNECTION_CHECK.PHP RUNS IN THE BACKGROUND
***/

$response = stream_get_contents($child);

if($response == "nocon") {
    die();
}
?>

任何帮助吗?如果任何人有另一种选择,这将是pciated以及AP $ P $。我一直特灵在一天的工作就这样了,并且没有运气。

Any help? If anybody has an alternative, that would be appreciated as well. I've been tring to work on this for over a day, and no luck yet.

推荐答案

在你的code您呼叫睡眠(3)的循环中,你的循环运行1000次,你有没有试过等待3000秒的循环来完成?我假设你是想检查每3秒,用户仍然连接。

In your code you are calling sleep(3) within your loop and your loop is running 1000 times, have you tried waiting 3000 seconds for the loop to finish? I assume you are wanting to check every 3 seconds that the user is still connected.

我的建议是删除睡眠,除去循环,让你的PHP基本检查连接只有一次,返回相关回应。呼叫通过AJAX,每X秒这个PHP。你可以看一下的setTimeout 的setInterval 在Javascript来实现这一目标。 AJAX调用完成后再次调用它的每一次也许设置超时。

My suggestion would be to remove the sleep, remove the loop and make your PHP essentially check the connection only once and return a relevant response. Call this PHP via AJAX, every X seconds. You can look at setTimeout and setInterval in Javascript to achieve this. Perhaps set a timeout every time the AJAX call is completed to call it again.

这样的事情(使用Javascript / jQuery的)

Something like this (Javascript/jQuery)

function checkConnection() {
    // Ajax call here
    $.ajax({
        // Other AJAX stuff
        complete: function() {
           setTimeout(function(){
               checkConnection();
           }, /*<YOURTIMEHERE>*/);
        }
    });
}

checkConnection();

甚至更好,你可以把setTimeout的位你成功的回调里面,当连接被认为是积极的。

Or even better you could put the setTimeout bit inside your success callback when the connection is considered active.

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

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