summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorRoberto De Ioris <info@unbit.it>2013-08-06 07:33:40 +0200
committerRoberto De Ioris <info@unbit.it>2013-08-06 07:33:40 +0200
commit8927dfd538c7f0d9307d82b37e53165d03755176 (patch)
tree01a51314a41baab4fac89af636f0f2c06023d176 /examples
parent68f734fcad4ac781968197d2c5f6a276ff918167 (diff)
downloaduwsgi-8927dfd538c7f0d9307d82b37e53165d03755176.tar.gz
moved example files
Diffstat (limited to 'examples')
-rw-r--r--examples/taskqueue.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/examples/taskqueue.py b/examples/taskqueue.py
new file mode 100644
index 00000000..121da38a
--- /dev/null
+++ b/examples/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"
+