首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >工作者中的Python uwsgi线程

工作者中的Python uwsgi线程
EN

Stack Overflow用户
提问于 2019-12-10 23:00:17
回答 1查看 4.8K关注 0票数 4

新的uwsgi,当我运行下面的代码时

代码语言:javascript
复制
from time import sleep
import threading
import os
import sys


i = 0


def daemon():
    print("pid ", os.getpid())
    global i
    while True:
        i += 1
        print("pid ", os.getpid(), i)
        sleep(3)


th = threading.Thread(target=daemon, args=())
th.start()

print("application pid ", os.getpid())


def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [str(i).encode()]


def atexit(*args):
    import uwsgi
    print('At exit called ', uwsgi.worker_id(), os.getpid())
    sys.exit(1)


try:
    import uwsgi
    uwsgi.atexit = atexit
except ImportError:
    pass

使用命令

代码语言:javascript
复制
uwsgi --http :9090 --wsgi-file  wsgi.py  --enable-threads --processes 2

我得到了输出

代码语言:javascript
复制
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 1418
your memory page size is 4096 bytes
detected max file descriptor number: 256
lock engine: OSX spinlocks
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :9090 fd 4
spawned uWSGI http 1 (pid: 37671)
uwsgi socket 0 bound to TCP address 127.0.0.1:62946 (port auto-assigned) fd 3
Python version: 3.7.4 
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145776 bytes (142 KB) for 2 cores
*** Operational MODE: preforking ***
pid  37670
application pid  37670
pid  37670 1
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fd29af02010 pid: 37670 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (pid: 37670, cores: 1)
spawned uWSGI worker 2 (pid: 37672, cores: 1)

pid  37670 2
pid  37670 3
pid  37670 4

我得到两个工作进程(37670和37672)。但是我创建的th = threading.Thread(target=daemon, args=())线程只在第一个工作进程(37670)中运行,而不是在37672中运行.

问题:

  1. 我原以为另一个线程也会在第二个工作进程中运行,但显然不是这样。它内部是如何工作的?
  2. 如何分别为每个工作进程和主进程创建线程(在本例中,我没有运行主进程)
EN

回答 1

Stack Overflow用户

发布于 2019-12-12 09:59:15

这似乎是因为预分叉。uwsgi的工作方式是只加载一次应用程序(因此只有一个解释器),然后根据所需的进程进行分叉。如果我们使用懒惰的应用程序,

代码语言:javascript
复制
uwsgi --http :9090 --wsgi-file  wsgi.py  --enable-threads --processes 2 --lazy-apps

这将在每个进程中加载一个应用程序,并且线程在工作人员processes.More info 这里中运行。

输出

代码语言:javascript
复制
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (pid: 14983, cores: 1)
spawned uWSGI worker 2 (pid: 14985, cores: 1)
pid 14983
pid 14985
Application pid 14983
Application pid 14985
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55e8d5282bb0 pid: 14985 (default app)
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55e8d5282bb0 pid: 14983 (default app)
pid 14985 1
pid 14983 1
pid 14985 2
pid 14983 2
pid 14985 3
pid 14983 3

通过在uwsgi中使用--线程选项,可以在每个进程中创建多个线程。

代码语言:javascript
复制
--processes 2 --threads 2

这将为每个进程创建2个线程。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59276594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档