From 3aae096ac28efc18a12b2a7bb8cb37aef42f9c62 Mon Sep 17 00:00:00 2001
From: Vincent Driessen <vincent@3rdcloud.com>
Date: Fri, 11 Nov 2011 17:18:06 +0100
Subject: [PATCH] Made a beginning to a Python module structure to support
 Redis queue.

@henrikuiper: Go ahead and experiment with this setup ;)
---
 __init__.py | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 __init__.py

diff --git a/__init__.py b/__init__.py
new file mode 100644
index 0000000..688fc86
--- /dev/null
+++ b/__init__.py
@@ -0,0 +1,18 @@
+from rdb import conn
+from pickle import loads, dumps
+
+def queue_daemon(app, queue_keys, rv_ttl=500):
+    """Naive implementation of a Redis queue worker, based on
+    http://flask.pocoo.org/snippets/73/
+
+    Will listen endlessly on the given queue keys.
+    """
+    while True:
+        msg = conn.blpop(queue_keys)
+        func, key, args, kwargs = loads(msg[1])
+        try:
+            rv = func(*args, **kwargs)
+        except Exception, e:
+            rv = e
+        if rv is not None:
+            conn.setex(key, rv_ttl, dumps(rv))