File: taskqueue.py

package info (click to toggle)
uwsgi 2.0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,564 kB
  • sloc: ansic: 87,066; python: 7,004; cpp: 1,133; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (39 lines) | stat: -rw-r--r-- 769 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from threading import Thread
import uwsgi

from six.moves import queue


CONSUMERS = 4


def consumer(q):
    while True:
        item = q.get()
        print(item)
        # ... DO A HEAVY TASK HERE ...
        q.task_done()


def spawn_consumers():
    global q
    q = queue.Queue()
    for i in range(CONSUMERS):
        t = Thread(target=consumer, args=(q,))
        t.daemon = True
        t.start()
        print("consumer %d on worker %d started" % (i, uwsgi.worker_id()))


uwsgi.post_fork_hook = spawn_consumers


def application(env, start_response):
    global q

    # we pass a copy of the env dictionary as it gets cleared after yield/return
    q.put(env.copy())

    start_response('200 OK', [('Content-Type', 'text/html')])

    yield "Task enqueued"