新的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中运行.
问题:
发布于 2019-12-12 09:59:15
这似乎是因为预分叉。uwsgi的工作方式是只加载一次应用程序(因此只有一个解释器),然后根据所需的进程进行分叉。如果我们使用懒惰的应用程序,
uwsgi --http :9090 --wsgi-file wsgi.py --enable-threads --processes 2 --lazy-apps这将在每个进程中加载一个应用程序,并且线程在工作人员processes.More info 这里中运行。
输出
*** 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中使用--线程选项,可以在每个进程中创建多个线程。
--processes 2 --threads 2这将为每个进程创建2个线程。
https://stackoverflow.com/questions/59276594
复制相似问题