简体   繁体   English

在脚本运行之前终止与浏览器的连接

[英]Kill connection with browser before script is through running

I have a script that takes an uploaded text file, parses it and then inserts quite a bit of rows into a database. 我有一个脚本,该脚本接收上载的文本文件,对其进行解析,然后将相当多的行插入数据库中。 This process takes about a minute. 此过程大约需要一分钟。

Even though I am using AJAX I don't want my users to have to see Loading... for upwards of a minute while the database is being populated. 即使我使用的是AJAX,我也不希望用户在填充数据库的过程中花一分钟以上的时间看到“正在加载...”。 Actually, as soon as the parsing completes successfully, the database INSERTS will always work because everything is validated during parsing. 实际上,一旦解析成功完成,数据库INSERTS将始终有效,因为在解析过程中所有内容均已验证。

So what I need to do is return a JSON string {success:true,data:{cartons: 1000, batchname:"Batch1"}} as soon as the parsing is successful. 因此,我需要做的就是在解析成功后立即返回一个JSON字符串{success:true,data:{cartons: 1000, batchname:"Batch1"}} I do this, but the browser doesn't parse the response until the connection is closed (script is through running) Is there anyway to get the browser to accept the response as it comes in or for me to close the connection with the browser while my PHP script continues to run? 我是这样做的,但是浏览器直到连接关闭(脚本正在运行)才解析响应(是否正在通过脚本运行),是否仍然可以让浏览器接受响应(当它进入时),或者让我关闭与浏览器的连接我的PHP脚本继续运行?

I am using ExtJS as the Javascript framework that is handling the AJAX. 我将ExtJS用作处理AJAX的Javascript框架。 Any ideas? 有任何想法吗? This has been racking my brain for about a week. 这已经让我绞尽脑汁了大约一个星期。

Just specify the length of the body content of the response: 只需指定响应内容的长度即可:

  $out = 'Return this';
  header("Content-Length: " . strlen($out));
  header("Connection: close");
  echo $out;
  flush();
  echo 'But do not return this';
  // create some file to show that script continues its execution 
  file_put_contents('script.is.running', '');

Also you can run other scripts from command line in the background (without waiting for their completion) or perform a http request to the script with ignore_user_abort function in it. 您也可以在后台从命令行运行其他脚本(无需等待它们的完成),或者对其中带有ignore_user_abort函数的脚本执行http请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM