Thrift 异步函数中的回调?

2022-01-03 00:00:00 asynchronous callback php thrift

在 Thrift 中,可以使用oneway修饰符将调用指定为异步.

In Thrift it is possible to use the oneway modifier to specify a call as asynchronous.

显然,无法定义回调,以便在函数执行完成时执行.

Apparently, it's not possible to define a callback, though, to be executed when the execution of the function is completed.

似乎我唯一的可能性是给我的 Thrift 客户端(PHP)一些服务器"功能,这样,当服务器端完成繁重的计算时,我可以发送给它的通知.这意味着我应该有一个新的 .thrift 文件,包含新的定义、新的服务和所有其他内容,并且我应该使用 Thrift 生成 php 服务器端代码.

It seems that the only possibility I have is to give my Thrift client (PHP) some "server" capabilities, so that, when the heavy computation is completed on the server side, I can send a notification to it. This means that I should have a new .thrift file, with new definitions, new services and all the rest and that I should generate php-server side code with Thrift.

即使这是可行的,对我来说也是一种矫枉过正,我想知道是否有更聪明的方法来实现回调.

Even if this is feasible, it looks like an overkill to me and I'm wondering if there's a more clever way to implement the callback.

期待你们的反馈,伙计们.

Looking forward for some feedback from you, guys.

推荐答案

Roberto,不幸的是 Thrift 框架没有这样的内置功能.不过,可能有多种替代方案,这取决于您希望 PHP 客户端会话在您通常等待计算密集型 Thrift 服务器响应期间执行的操作(如果您没有使用oneway.)

Roberto, unfortunately the Thrift framework has no such built-in functionality. There may be a number of alternatives, though, depending on what you want your PHP client session to do during the time you would normally have waited for the computationally-intensive Thrift server to answer (had you not used oneway.)

我现在只能想象,如果您编写了一个 Web 应用程序,其中一个用户(或多个并行用户)可以触发计算密集型任务,您想提供一些反馈在所述任务不断进行的同时向所述用户发送.

I can only imagine, for now, that you are in a situation where, having coded a web application where a user (or several users in parallel) can each trigger a computationally-intensive task, you would like to provide some feedback to said users while said tasks churn along.

从一开始,您就完全正确地试图避免您试图避免的解决方案.您的 PHP 客户端会话无法在不阻塞的情况下为回调接口提供服务(除非您尝试使用 pcntl_fork 或其他一些PHP 线程创可贴.)

From the start, you are absolutely right in trying to avoid the solution that you are trying to avoid. Your PHP client sessions cannot service a callback interface without blocking (unless you dig your hole even deeper by trying to use pcntl_fork or some other PHP threading band-aid.)

最简单和恕我直言最好的方法是两个从事件驱动模型切换(我想在服务器完成时收到通知)轮询模型(我会定期询问服务器是否完成.)有几种实现轮询模型的方法,服务器上也有多种实现选项在客户端,例如:

The simplest and IMHO best way out of this is two switch from an event-driven model (I want to be notified when the server is done) to a polling model (I will periodically inquire with the server whether or not it is done.) There are several ways of implementing a polling model, with multiple implementation options on the server as well as on the client sides, such as:

  1. 在调用阶段:

  • PHP 客户端会话分配一个唯一的 job_id 值;然后会话使异步 oneway 调用 void compute(..., job_id) 到计算密集型 Thrift 服务器,
  • the PHP client session allocates a unique job_id value; the session then makes the asynchronous oneway call void compute(..., job_id) to the computationally-intensive Thrift server,

-- 或 --

  • PHP 客户端会话对计算密集型 Thrift 服务器进行同步调用job_id start_compute(...);服务器分配唯一的 job_id 值,然后在单独的线程/进程中生成实际的计算密集型任务,使用分配的 job_id<立即返回到 PHP 客户端会话/li>
  • the PHP client session makes a synchronous call job_id start_compute(...) to the computationally-intensive Thrift server; the server allocates the unique job_id value, then spawns the actual computationally-intensive task in a separate thread/process, returning right away to the PHP client session with the allocated job_id

在计算阶段:

  • PHP 客户端会话通过对计算密集型 Thrift 服务器的同步 status get_status(job_id) 调用,定期检查计算密集型作业的状态,
  • the PHP client session proceeds to periodically check the status of the computationally-intensive job via a synchronous status get_status(job_id) call to the computationally-intensive Thrift server,

-- 或 --

  • PHP 客户端会话立即终止以释放宝贵的资源,将 job_id 传递给浏览器并指示浏览器定期检查计算密集型作业的状态 <代码>job_id(例如通过META REFRESH,或通过来自 Javascript 等的 XHR (AJAX) 请求);浏览器检查会产生一个简短的 PHP 客户端会话,该会话执行对计算密集型 Thrift 服务器的同步 status get_status(job_id) 调用,在转发状态后立即终止(无论哪个可能)在浏览器上
  • the PHP client session terminates right away in order to free up precious resources, after passing on the job_id to the browser and also instructing the browser to periodically check the status of the computationally-intensive job job_id (e.g. via META REFRESH, or via an XHR (AJAX) request from Javascript, etc.); the browser check spawns a brief PHP client session which performs the synchronous status get_status(job_id) call to the computationally-intensive Thrift server, terminating immediately after forwarding the status (whichever it may be) on to the browser

相关文章