Basically, background processing should start outside the WSGI application module.
WSGI webapp python , . , WSGI , .
, WSGI , WSGI. - . WSGI webapp, .
your_app/webapp.py:
from . import bg_queue
def post():
bg_queue.add(<task data>)
your_app/processor.py:
from multiprocessing import Process
class Consumer(Process):
def __init__(self, input_q):
self.input_q = input_q
def run(self):
while True:
task_data = input_q.get()
<process data>
your_app/__init__.py:
from .processor import Consumer
bg_queue = Queue()
consumer = Consumer(bg_queue)
consumer.daemon = True
consumer.start()