如何扩展我的Django应用程序以利用4个核心和8个线程?

huangapple go评论48阅读模式
英文:

How can I scale up my django app to utilize 4 cores and 8 threads?

问题

我有一个Django应用。我即将将其从单核系统迁移到多核系统(8个线程,因为支持超线程)。

我正在努力弄清楚需要进行的更改,以利用所有核心和所有线程。我四处查找了一些Rails的设置,但在网上找不到Django的类似设置。

我找到一篇文章说,我不需要对Django进行任何更改。我只需使用gunicorn运行8个实例以利用8个线程,但这对我来说没有意义。

我需要进行哪些更改,以确保我的应用程序利用8个线程而不浪费资源。

英文:

I have a Django app. I am about to move this from a single core system to a multi core system. (8 threads because of hyperthreading)

I am trying to figure out the changes I need to make to utilize all of the cores and all of the threads. I looked around and found bunch of settings for Rails. I can not find similar settings for Django online.

I found an article that says, I don't need to make any changes to Django. I should just use gunicorn to run 8 instances to utilize 8 threads, but this did not make sense to me.

What do I need to change to make sure my app utilizes 8 threads without wasting resources.

答案1

得分: 1

这是最简单的方法,绝对有道理。您将拥有多个应用程序实例,负载将被平衡。操作系统将利用多个核心来运行您的应用程序的多个进程。

英文:

> I should just use gunicorn to run 8 instances to utilize 8 threads,
> but this did not make sense to me.

this is the easiest way and it definitely makes sense. You will have multiple instances of your app and load will be balanced. OS will utilize multiple cores to run multiple processes of your app.

答案2

得分: 1

Django本身是一个单线程的Web框架,但可以部署在多核服务器上,以同时处理多个请求。

您可以

  1. 使用Web服务器。 Django可以部署在Web服务器上,如ApacheNginxGunicorn。 这些Web服务器可以配置为生成多个工作进程或线程以处理传入请求。 每个工作进程/线程可以同时处理一个请求,多个请求可以由多个工作进程同时处理。
  2. 使用WSGI服务器。 Django还可以部署在WSGI(Web服务器网关接口)服务器上,如uWSGImod_wsgi。 WSGI服务器专门设计用于处理Python Web应用程序,并可以生成多个工作进程/线程以处理传入请求。
  3. 或者使用ASGI。 异步服务器网关接口(ASGI)是Python中用于异步Web服务器和框架的标准接口。 ASGI允许以非阻塞方式处理多个同时连接。 Django 3.0及更高版本支持ASGI,并可以用于将Django部署在支持ASGI的Web服务器上,如Daphneuvicorn

您还可以考虑将一些繁重的任务交给Celery来处理。

英文:

Django itself is a single-threaded web framework, but it can be deployed on multi-core servers to handle multiple requests simultaneously.

You can

  1. use a Web Server. Django can be deployed on a web server such as Apache, Nginx, or Gunicorn. These web servers can be configured to spawn multiple worker processes or threads to handle incoming requests. Each worker process/thread can handle a single request at a time, and multiple requests can be handled concurrently by multiple workers.
  2. use a WSGI Server. Django can also be deployed on a WSGI (Web Server Gateway Interface) server such as uWSGI or mod_wsgi. WSGI servers are specifically designed to handle Python web applications and can spawn multiple worker processes/threads to handle incoming requests.
  3. or use ASGI. Asynchronous Server Gateway Interface (ASGI) is a standard interface for asynchronous web servers and frameworks in Python. ASGI allows for multiple simultaneous connections to be handled in a non-blocking way. Django 3.0 and later versions support ASGI, and it can be used to deploy Django on an ASGI-compatible web server such as Daphne or uvicorn.

You could potentially offload some heavy stuff to Celery.

huangapple
  • 本文由 发表于 2023年2月18日 18:25:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定