从异步 API 获取结果 [英] Obtaining result from async API

查看:36
本文介绍了从异步 API 获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建用于使用 Lumen 进行处理的 API,每个请求的处理作业大约需要 1-3 秒.

I am building API for processing with Lumen, the processing job is taking around 1-3 seconds per request.

到目前为止,我是用作业队列和 beanstalkd 完成的,它是异步的,这意味着我返回 job_id,稍后我可以检查结果.

So far i did it with job queue and beanstalkd and it is asynchronous, meaning i return job_id that i can check later for the result.

我也在编写 PHP 客户端来利用 API,为此我想知道我是否应该包括等待"参数服务器端或客户端?如果在服务器端实现等待,我将需要在作业分派后休眠并检查数据库中的结果,然后在可用时返回结果(在接下来的 1-5 秒内直到可用),或者如果是客户端,我将需要休眠和如果作业完成并获得结果,请通过特定路线检查 job_id.

I am also writing PHP client to utilize the API and for that i am wondering if i should include 'wait' parameter server side or client side? If wait is implemented serverside i will need to sleep and check the database for results once the job is dispatched and then return result when available (in next 1-5 seconds until available), or if it is client side i will need to sleep and check with the job_id via specific route if the job finished and to get the results.

哪个选项更好?

推荐答案

我会有一个端点,它的唯一工作是检查作业 ID 并等待.没有选择在其他端点上等待结果,因为它会破坏异步性,消费者总是等待,因为它更容易.

I would have an endpoint which its sole job is to check job ids and wait. There would be no option to wait for the result on other endpoints as it will break the asynchronicity, consumers would just always wait as its easier.

好的,客户端将发送作业并取回作业 ID.

Ok, the client would send the job and get a job id back.

然后您将该作业 ID 发送到等待端点,该端点将等待/挂起,直到作业完成或失败.然后你可以再次查询作业并得到结果.

Then you send that job id to a wait endpoint, the endpoint will wait/hang till the job is complete or fails. Then you can query the job again and get the result.

这将防止需要轮询服务器,并且还可以防止客户端因需要休眠、轮询、休​​眠而阻塞.

That will prevent the need to poll the server, and also prevent blocking in the client by needing to sleep, poll, sleep.

有点像 LXD 操作端点:https:///github.com/lxc/lxd/blob/master/doc/rest-api.md#10operationsuuidwait

Abit like LXD operations endpoint: https://github.com/lxc/lxd/blob/master/doc/rest-api.md#10operationsuuidwait

这篇关于从异步 API 获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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