2011-11-14 14:15:05 +00:00
|
|
|
import os
|
|
|
|
import time
|
2011-11-15 21:45:51 +00:00
|
|
|
from rq import Queue
|
2011-11-14 14:15:05 +00:00
|
|
|
from fib import slow_fib
|
|
|
|
|
2011-11-15 21:45:51 +00:00
|
|
|
# Tell RQ what Redis connection to use
|
|
|
|
from redis import Redis
|
|
|
|
from rq import conn
|
2011-11-14 23:35:18 +00:00
|
|
|
conn.push(Redis())
|
2011-11-14 14:15:05 +00:00
|
|
|
|
2011-11-15 21:45:51 +00:00
|
|
|
# Range of Fibonacci numbers to compute
|
|
|
|
fib_range = range(20, 34)
|
|
|
|
|
2011-11-14 23:35:18 +00:00
|
|
|
# Kick off the tasks asynchronously
|
|
|
|
async_results = {}
|
2011-11-15 21:45:51 +00:00
|
|
|
q = Queue()
|
|
|
|
for x in fib_range:
|
|
|
|
async_results[x] = q.enqueue(slow_fib, x)
|
2011-11-14 14:15:05 +00:00
|
|
|
|
2011-11-15 21:45:51 +00:00
|
|
|
start_time = time.time()
|
2011-11-14 23:35:18 +00:00
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
os.system('clear')
|
2011-11-15 21:45:51 +00:00
|
|
|
print 'Asynchronously: (now = %.2f)' % (time.time() - start_time)
|
2011-11-14 23:35:18 +00:00
|
|
|
done = True
|
2011-11-15 21:45:51 +00:00
|
|
|
for x in fib_range:
|
2011-11-14 23:35:18 +00:00
|
|
|
result = async_results[x].return_value
|
|
|
|
if result is None:
|
|
|
|
done = False
|
|
|
|
result = '(calculating)'
|
|
|
|
print 'fib(%d) = %s' % (x, result)
|
|
|
|
print ''
|
|
|
|
print 'To start the actual in the background, run a worker:'
|
|
|
|
print ' python examples/run_worker.py'
|
2011-11-15 21:45:51 +00:00
|
|
|
time.sleep(0.2)
|
2011-11-14 14:15:05 +00:00
|
|
|
|
2011-11-14 23:35:18 +00:00
|
|
|
print 'Done'
|