worker 中的 Python uwsgi 线程

标签 python multithreading uwsgi

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 中运行...

题:
  • 我期待另一个线程也在第二个工作进程中运行,但显然情况并非如此。它在内部如何运作?
  • 我如何为每个工作进程和主进程分别创建线程(在这个例子中,我没有运行主进程)
  • 最佳答案

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

    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 个线程。

    关于 worker 中的 Python uwsgi 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59276594/

    相关文章:

    python - 使用 Python 处理来自 MySQL 的数据

    python - 如何使用 Python 生成一个给定长度的文件,其中填充了随机内容?

    sockets - 未创建 uwsgi 套接字

    python-3.x - uWSGI 不会切换到自定义 uwsgi.ini 文件中定义的用户 uid=my_user

    在字典整数映射中查找单独整数间隔的Pythonic方法

    java - 调用 SwingUtilities.invokeAndWait() 时创建的新线程?

    java - 在 Java 中使用线程绘制面板

    c - 这个简单的非阻塞队列会失败吗? (在带有 gcc atomic CAS 的 C 语言中)

    python - uwsgi : doesn't detect plugins

    Python Turtle 窗口每第二次运行就会崩溃