[英]Should I use Laravel Queues to manage threads across my application
I am looking to hit multiple 3rd party APIs to gather information for a user's search query. 我正在寻找打多个第三方API来收集用户搜索查询的信息。 I am planning to spin off a thread for each API I want to hit to minimize the response time on my end.
我正计划为每个要击中的API分派一个线程,以最大程度地缩短响应时间。 I also want to limit the amount of threads my application can have running at any one time due to memory/cpu concerns.
由于内存/ CPU问题,我还想限制我的应用程序可以在任何时候运行的线程数量。
Since I am using Laravel as my framework, I was trying to accomplish this using Laravel queues, but it seems that I might have trouble getting the response data from the Job. 由于我使用Laravel作为框架,因此我试图使用Laravel队列来完成此任务,但是似乎我可能无法从Job那里获得响应数据。
It depends. 这取决于。 A job queue and worker pool might be appropriate if there are a really huge number of API calls to make, especially if those API calls can be very slow.
如果要进行大量的API调用,特别是如果这些API调用可能非常慢的话,则作业队列和工作池可能是合适的。 But, I'd try to avoid all that architecture unless you're really sure you need it.
但是,除非您确实确定需要,否则我将尽量避免使用所有架构。
To start, I'd look at doing async requests to the external APIs, and try to keep the whole thing in a single process. 首先,我将研究对外部API的异步请求,并尝试将整个过程保持在一个过程中。 The Guzzle HTTP client library provides a very programmer-friendly API for doing this kind of asynchronous requests.
Guzzle HTTP客户端库提供了一种非常友好的API,可以进行这种异步请求。
If the external requests are really numerous or slow, you might consider using a queue. 如果外部请求确实很多或很慢,则可以考虑使用队列。 But in that case, you're looking at implementing a bunch of logic to queue all the jobs, then poll until they're done (giving feedback to your user along the way), and finally return the merged result.
但是在那种情况下,您正在考虑实现一堆逻辑来对所有作业进行排队,然后进行轮询直到完成(将反馈反馈给用户),最后返回合并的结果。 That may end up being necessary, but I'd start with the simpler implementation I describe above.
可能最终有必要这样做,但是我将从上面描述的更简单的实现开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.