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

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

问题描述

我正在构建用于通过Lumen处理的API,每个请求的处理工作大约需要1-3秒.

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

到目前为止,我是通过作业队列和beantalkd完成的,并且它是异步的,这意味着我返回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,为此,我想知道是否应该在服务器端或客户端包含"wait"参数?如果在服务器端实施了等待,则一旦分配工作,我将需要休眠并检查数据库的结果,然后在可用时返回结果(在接下来的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天全站免登陆