summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorVincent Driessen <vincent@3rdcloud.com>2011-11-15 22:45:51 +0100
committerVincent Driessen <vincent@3rdcloud.com>2011-11-15 22:45:51 +0100
commit22f3da1832ff69e8fae7b9a6e0d0ff3994b3a334 (patch)
tree42b1e17b1819beab3c21ac64dc8b602c03604b4f /examples
parent1a893e60cf46f8340a6a45e42c45e163172a07cd (diff)
downloadrq-22f3da1832ff69e8fae7b9a6e0d0ff3994b3a334.tar.gz
Update README (and example).
Diffstat (limited to 'examples')
-rw-r--r--examples/fib.py3
-rw-r--r--examples/run_example.py22
-rw-r--r--examples/run_worker.py11
3 files changed, 20 insertions, 16 deletions
diff --git a/examples/fib.py b/examples/fib.py
index 521e06d..2130b3c 100644
--- a/examples/fib.py
+++ b/examples/fib.py
@@ -1,6 +1,3 @@
-from rq import job
-
-@job('default')
def slow_fib(n):
if n <= 1:
return 1
diff --git a/examples/run_example.py b/examples/run_example.py
index f3414d3..d1b273c 100644
--- a/examples/run_example.py
+++ b/examples/run_example.py
@@ -1,23 +1,29 @@
import os
import time
-from rq import conn
-from redis import Redis
+from rq import Queue
from fib import slow_fib
-# Tell rq what Redis connection to use
+# Tell RQ what Redis connection to use
+from redis import Redis
+from rq import conn
conn.push(Redis())
+# Range of Fibonacci numbers to compute
+fib_range = range(20, 34)
+
# Kick off the tasks asynchronously
async_results = {}
-for x in range(20, 30):
- async_results[x] = slow_fib.delay(x)
+q = Queue()
+for x in fib_range:
+ async_results[x] = q.enqueue(slow_fib, x)
+start_time = time.time()
done = False
while not done:
os.system('clear')
- print 'Asynchronously: (now = %s)' % time.time()
+ print 'Asynchronously: (now = %.2f)' % (time.time() - start_time)
done = True
- for x in range(20, 30):
+ for x in fib_range:
result = async_results[x].return_value
if result is None:
done = False
@@ -26,6 +32,6 @@ while not done:
print ''
print 'To start the actual in the background, run a worker:'
print ' python examples/run_worker.py'
- time.sleep(1)
+ time.sleep(0.2)
print 'Done'
diff --git a/examples/run_worker.py b/examples/run_worker.py
index 3efc472..e87ec89 100644
--- a/examples/run_worker.py
+++ b/examples/run_worker.py
@@ -1,9 +1,10 @@
-from redis import Redis
-from rq import conn
-from rq.daemon import run_daemon
+from rq import Queue, Worker
# Tell rq what Redis connection to use
+from redis import Redis
+from rq import conn
conn.push(Redis())
-listen_on_queues = ['default']
-run_daemon(listen_on_queues)
+if __name__ == '__main__':
+ q = Queue()
+ Worker(q).work_forever()