工作者中的Python uwsgi线程

问题描述 投票:0回答:1

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

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

使用命令

uwsgi --http :9090 --wsgi-file  wsgi.py  --enable-threads --processes 2

我得到输出

*** 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. 我如何分别为每个辅助进程和主进程创建线程(在此示例中,我没有运行主进程)
python multithreading uwsgi
1个回答
0
投票

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

uwsgi --http :9090 --wsgi-file  wsgi.py  --enable-threads --processes 2 --lazy-apps

这将为每个进程加载一个应用程序,并且线程在两个工作进程中运行。更多信息here

输出

*** 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中的--threads选项为每个进程创建多个线程

--processes 2 --threads 2

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

© www.soinside.com 2019 - 2024. All rights reserved.