PHP的异步调用,并从后台作业得到响应 [英] php asynchronous call and getting response from the background job

查看:161
本文介绍了PHP的异步调用,并从后台作业得到响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一些关于这个论题谷歌搜索,但没有找到答案我的问题。

I have done some google search on this topic and couldn't find the answer to my question.

我想实现的是以下内容:

What I want to achieve is the following:


  1. 客户端做出一个函数异步调用服务器

  2. 服务器在后台运行该函数(因为该函数是费时),并且客户端不挂在其间

  3. 客户端不断向服务器请求后台作业的状态通话

您可以给我一些建议关于解决我的问题?

Can you please give me some advices on resolving my issue?

非常感谢你! ^ - ^

Thank you very much! ^-^

推荐答案

您没有指定异步调用是什么语言,但我假设两端PHP。
我觉得最优雅的方式是这样的:

You are not specifying what language the asynchronous call is in, but I'm assuming PHP on both ends. I think the most elegant way would be this:


  • HTML页面加载,定义了一个随机密钥的操作(例如,使用兰特()或已经可用的会话ID [要小心,虽然是同一个用户可以启动两个操作])

  • HTML page loads, defines a random key for the operation (e.g. using rand() or an already available session ID [be careful though that the same user could be starting two operations])

HTML网页,使Ajax调用PHP脚本 start_process.php

HTML page makes Ajax call to PHP script to start_process.php

start_process.php 执行 EXEC /path/to/scriptname.php 启动该进程;看到 EXEC 用户投稿注意事项( ) 的建议,如何在后台启动一个进程。哪一个是你的权利,主要取决于你的操作系统。

start_process.php executes exec /path/to/scriptname.php to start the process; see the User Contributed Notes on exec() on suggestions how to start a process in the background. Which one is the right for you, depends mainly on your OS.

long_process.php 经常撰写的身份进入一个状态文件,生成您的Ajax页面随机密钥来命名

long_process.php frequently writes its status into a status file, named after the random key that your Ajax page generated

HTML页面将频繁调用 show_status.php 的读出状态文件,并返回进度。

HTML page makes frequent calls to show_status.php that reads out the status file, and returns the progress.

这篇关于PHP的异步调用,并从后台作业得到响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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