如何让uWSGI偏爱进程而不是线程来进行负载平衡 [英] How to tell uWSGI to prefer processes to threads for load balancing

查看:716
本文介绍了如何让uWSGI偏爱进程而不是线程来进行负载平衡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在具有3个CPU内核的VDS上安装了Nginx + uWSGI + Django. uWSGI配置为6个进程,每个进程5个线程.现在,我要告诉uWSGI使用进程进行负载均衡,直到所有进程都忙,然后在需要时使用线程.看来uWSGI更喜欢线程,并且我没有找到任何配置选项来更改此设置行为.第一个进程占用了100%的CPU时间,第二个进程占用了约20%的CPU时间,而大多数不使用其他进程.

I've installed Nginx + uWSGI + Django on a VDS with 3 CPU cores. uWSGI is configured for 6 processes and 5 threads per process. Now I want to tell uWSGI to use processes for load balancing until all processes are busy, and then to use threads if needed. It seems uWSGI prefer threads, and I have not found any config option to change this behaviour. First process takes over 100% CPU time, second one takes about 20%, and another processes are mostly not used.

我们的站点收到40 r/s.实际上,即使有3个没有线程的进程也足以应付所有请求.但是由于各种原因(例如共享资源锁定等),请求处理会不时挂起.在这种情况下,我们有-1个进程.用户不想等待并一次又一次单击链接.结果,所有进程都挂起,所有用户都必须等待.

Our site receives 40 r/s. Actually even having 3 processes without threads is anough to handle all requests usually. But request processing hangs from time to time for various reasons like locked shared resources, etc. In such cases we have -1 process. Users don't like to wait and click the link again and again. As a result all processes hangs and all users have to wait.

我将添加更多线程以使服务器更强大.但是问题可能出在python GIL上.线程不会使用所有CPU内核.因此,多个进程可以更好地实现负载平衡.但是在共享资源锁定和I/O等待延迟的情况下,线程可能会很有帮助.进程在其中一个线程被锁定时可能会做很多工作.

I'd add even more threads to make the server more robust. But the problem is probably python GIL. Threads wan't use all CPU cores. So multiple processes work much better for load balancing. But threads may help a lot in case of locked shared resources and i/o wait delays. A process may do much work while one of it's thread is locked.

在没有其他解决方案之前,我不想减少时间限制.从理论上讲,可以通过线程解决此问题,并且我不想向用户显示错误消息或不想让他等待每个请求,直到没有其他选择为止.

I don't want to decrease time limits until there is no another solution. It is possible to solve this problem with threads in theory, and I don't want to show error messages to user or to make him waiting on every request until there is no another choice.

推荐答案

因此,解决方案是:

  1. 将uWSGI升级到最新的稳定版本(如roberto建议).
  2. 使用--thunder-lock选项.

现在我每个进程运行50个线程,并且所有请求平均分配在各个进程之间.

Now I'm running with 50 threads per process and all requests are distributed between processes equally.

这篇关于如何让uWSGI偏爱进程而不是线程来进行负载平衡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆