From 733077a4377e83e8c8ab091b4ea1f7bc0b27fc39 Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Sun, 26 Jun 2011 18:18:57 +0200 Subject: [PATCH] added another example --- taskqueue.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 taskqueue.py diff --git a/taskqueue.py b/taskqueue.py new file mode 100644 index 00000000..121da38a --- /dev/null +++ b/taskqueue.py @@ -0,0 +1,36 @@ +import Queue +from threading import Thread +import uwsgi + +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" +