简体   繁体   English

Laravel:使用 HttpClient 的多个作业的解决方案?

[英]Laravel: Solution for multiple jobs using HttpClient?

i have a bunch of laravel jobs who connect to an external API, fetch some data and processes the data.我有一堆 laravel 连接到外部 API 的工作,获取一些数据并处理数据。 Because it is very different data, i splitted it into multiple jobs (GET_DATA_A, GET_DATA_B, ...)因为它是非常不同的数据,所以我将它分成多个作业(GET_DATA_A,GET_DATA_B,...)

But every jobs creates a new HttpClient (Guzzle) and a new connection to the API, which makes the API very slow.但是每个作业都会创建一个新的 HttpClient (Guzzle) 和一个到 API 的新连接,这使得 API 非常慢。

Is there a solution to use one HttpClient (with keep-alive) for all jobs?是否有针对所有作业使用一个 HttpClient(保持活动状态)的解决方案?

Best regards最好的祝福

This is possible to achieve because the queue worker is a long running process.这是可能实现的,因为队列工作者是一个长时间运行的进程。

As long as your settings for the queue worker are not causing it to exit early (such as a low max job count, or low max time) it should be possible.只要您对队列工作者的设置不会导致它提前退出(例如,最大作业数较低或最大时间较低),它应该是可能的。

You need to use the singleton pattern to reuse the guzzle client.您需要使用 singleton 模式来重用 guzzle 客户端。 Then you can reuse the same client across your jobs.然后您可以在您的作业中重复使用同一个客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM