简体   繁体   English

我是否应该使用Laravel队列来管理应用程序中的线程

[英]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那里获得响应数据。

  • Are laravel queues the correct way to tackle this? laravel队列是否是解决此问题的正确方法? If so how do I listen for the job's status and retrieve the data once the job is complete? 如果是这样,我如何侦听作业的状态并在作业完成后检索数据? I see some things that point towards passing a closure to the job, but something just isn't clicking for me. 我发现有些事情会导致关闭工作,但是有些事情并没有为我带来好处。

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.

相关问题 我应该使用 Laravel 队列和 Ajax 发送批量电子邮件吗? - Should I Use Laravel Queues With Ajax For Sending Bulk Emails? 如何在部署中管理 Laravel 队列? - How to manage Laravel queues on deployments? 我应该将Laravel 4应用程序更新为Laravel 5还是创建新的Laravel 5应用程序 - Should I update my Laravel 4 application to Laravel 5 or create a new Laravel 5 application Laravel 队列和跨类的持久变量 - Laravel queues & Persistent Variables across classes Laravel应用程序结构:什么时候应该使用模型,什么时候应该使用帮助器类? - Laravel Application Structure: When should I use a model, and when should I use a helper class? 我应该在我的 Laravel 应用程序中使用什么层或图案? - What layer or pattern should I use in my Laravel app? 我可以使用laravel Queues自动删除一些表记录吗? - can i use laravel Queues to delete some table records automatically? 我可以使用Laravel 4队列在Windows上处理打印作业吗? - Can I use Laravel 4 Queues to process print-jobs on Windows? 我应该在论坛地图中加入论坛主题吗? - Should I put forum threads into my sitemap? 我应该使用什么数据类型在MySQL中为我的应用程序存储time()? - What datatype should I use to store time() in MySQL for my application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM