Puma中的Workers和Threads有什么区别

Puma中的Workers和Threads有什么区别

本文介绍了Puma中的Workers和Threads有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 heroku dyno 的上下文中,puma worker 和 puma 线程有什么区别?

What is the difference between a puma worker and a puma thread in context of a heroku dyno?

我所知道的(如果我错了,请纠正我):

What I know (please correct me if I am wrong):

  • Thin 不是并发的,所以一个 web 进程一次只能做一个请求

  • Thin is not concurrent, so a web process can only do one request at a time

在 unicorn 中,我知道我可以在一个进程中使用多个 unicorn worker 来增加并发性.

In unicorn, I know I can have several unicorn workers in one process to add concurrency.

但是在 puma 中有线程和工人.. 工人不是 puma 进程中的线程吗?

But in puma there is threads and workers.. Isn't a worker a thread inside the puma process?

我可以使用更多的工作线程/线程在 Heroku 中添加 Web 并发吗?

Can I use more workers/threads to add web concurrency in Heroku?

推荐答案

正如其他答案所述,这 Heroku 文章 对某些配置项的解释非常好.

As the other answer states, this Heroku article is pretty good with explanations of certain configuration items.

但是,如果您需要在 Heroku 或任何地方调整您的应用程序,那么了解工作原理是值得的.

However if you need to tune your application on Heroku, or anywhere, then it pays to know how things work.

我认为您说worker 是 puma 进程内的线程"几乎是正确的,我相信 worker 是从 puma 派生出来的操作系统级进程,然后可以在内部使用线程.

I think you are almost correct when you say "a worker is a thread inside the puma process", I believe a worker is an operating system level process forked from puma which then can use threads internally.

据我所知 - 无论您通过 workers 配置设置多少次,puma 都会分叉其操作系统进程以响应 http 请求.这为您提供了处理多个请求的并行性,但这通常会占用更多内存,因为它会为每个工作人员复制"您的应用程序代码.

As far as I understand - puma will fork its operating system process however many times you set via workers configuration to respond to http requests. This gives you parallelism in terms of handling multiple requests but this will usually take up more memory as it will 'copy' your application code for each worker.

然后,每个 puma worker 将根据 threads 配置在其操作系统进程中使用多个线程.这些通过允许 puma 进程自己响应多个请求来增加并发性,这样如果一个线程被阻塞,即处理一个请求,它可以用另一个线程处理一个新请求.如前所述,这要求您的整个应用程序都是线程安全的,例如,来自一个请求的任何全局配置都不会泄漏"到另一个请求中.

Each puma worker will then use multiple threads within its OS process depending on the threads configuration. These add concurrency by allowing the puma process to respond to multiple requests itself so that if one thread is blocked, ie processing a request, it can handle a new request with another thread. As stated, this requires your entire application to be threadsafe so that, for example any global configuration from one request does not 'leak' into another.

您可以调整 puma 以使工作线程的数量足以满足可用的 CPU 和内存数量,然后根据您希望使运行应用程序的主机达到饱和程度以及应用程序的行为方式来调整线程——更多并不总是等于更快/更多的请求吞吐量.

You would tune puma so that the number of workers was adequate for the number of CPUs and memory available and then tune the threads dependant on how much you would want to saturate the host running your application and how your application behaves - more does not always equal faster/more request throughput.

这篇关于Puma中的Workers和Threads有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 17:49