立即将数据发送到客户端,并继续PHP处理

时间:2015-03-30 22:16:58

标签: php ajax asynchronous

是否可以在PHP中立即将 发送到客户端,并继续PHP处理(可能会阻止)?

<?php

// some code 

echo json_encode(array('ok' => '1'));  // the client is waiting for this AJAX answer !!
// how to send the response right now before finishing this PHP file ?

// the output is REALLY finished here, so client, you can work with it

some_blocking_processing();  // this is just some server processing that would
                             // block the client for ~ 5 seconds
                             // but it produces no output useful for client

?>

我知道正确的方法可能是使用队列或其他进程来执行处理。

但是,作为一个通用的问题,是否有可能将数据发送到客户端,在PHP文件的中间

1 个答案:

答案 0 :(得分:1)

那真的取决于some_blocking_processing()实际上在做什么。我不能在不知道那里发生了什么的情况下提出解决方案。

但是我可以指出一些你可以做更多研究的领域。其中一个可能是适合您的解决方案:

  1. PHP threading
  2. 产生异步php进程
  3. 在file / db中记录您的状态,然后通过cron作业进行额外处理