php - 运行长 php 脚本的最佳实践
问题描述
我试图弄清楚如何在不让用户永远等待的情况下运行长 php 脚本。我有一个脚本需要运行几个函数,每个函数都需要几秒钟才能运行。
我想知道有没有一种方法可以让我的 php 脚本在完成运行之前将其数据返回给调用者?
<?php
echo http_response_code(200);
sleep(60);
run_other_code();
?>
一个非常简单的示例,但我正在寻找脚本来回显响应,然后在后台运行其余代码。这样的事情可能吗?
解决方案
使用 ajax 调用来执行此类任务,请看下面的示例
$.ajax({
url: "getData.php",
error: function(){
// this code will fire when timeout is reached
},
success: function(){
//this code will fire when the script is finished doing the task
},
timeout: 61000 // always kepp it more than the time total time taken by your script
});
希望能帮助到你
推荐阅读
- r - R中的子集数据框仅适用于某些值
- html - Swiftsoup 解析未找到所有 HTML 类
- .net - 是否有在 Linux Mint 19.3 上下载和安装 .NET Core 3.1 SDK 的说明?
- java - 如果我的设备上的移动后台数据受到限制,Firebase 身份验证将不起作用
- macos - Mac 终端自动完成提示文件不在当前目录中
- python - 默认值未在 tkinter 单选按钮中设置
- python-3.x - python io.BytesIO处理大内容的问题
- python - 如何在 Python 中通过 ZeroMQ PUB/SUB 从 Raspberry Pi 接收图像?
- python - Python根据多维字典中的值添加值的最佳方法
- node.js - 在 Express 中分离 app.use() 与链式 app.use().use()...