PHP 保持进程存活
我正在努力寻找解决方案来解决用户访问 page1.php 并且该页面包含另一个 page2.php 需要花费大量时间(约 5 分钟)来完成所有计算但计算结果并不需要显示给用户。因此,让用户等待其完成是不值得的。
我正在寻找一种解决方案,用户应访问 page1.php,并在进行一些必要的检查列表后,它应向用户显示其请求处理已开始的消息。在后端 page2.php 应该完成其所有工作,即使用户应该关闭窗口。
I am struggling to find a solution to a problem where a user visits to page1.php and this page include another page2.php which takes lot of time (~5 minute) to complete all the computation but the results of the computation doesn't need to be displayed to user. So, its not worth to make the user to wait for its completion.
I am looking for a solution where user should visit page1.php and after some necessary check listing it should display message to the user that his request processing has started. And on the back end page2.php should complete all of its working even if user should have closed window.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将实际计算卸载到独立进程(cron 作业或自定义守护进程)并实现某种作业队列(例如,使用数据库)。因此,您的前端(PHP 网站)会将数据添加到数据库表中,然后定期检查它(例如,通过读取“进度”列或其他内容);处理守护进程将在表中查找是否有任何新作业,如果有,则开始处理它们,并根据需要更新表。
Offload the actual computation to a standalone process (a cron job, or a custom daemon) and implement some sort of job queue (e.g., using a database). So your front-end (the PHP website) would add the data to a database table, and then periodically check back on it (e.g. by reading a 'progress' column or something); the processing daemon would look in the table to see if there are any new jobs, and if so, start processing them, updating the table as needed.
查看此链接 如何发出异步 GET 请求在 PHP 中?
如果您想在请求页面上显示结果,请使用 file_get_contents
check out this link How do I make an asynchronous GET request in PHP?
if you would like to get the result displayed on the request page, use file_get_contents