Rewrite of the connection setup.

I'd want to advice against using `use_connection()` more, as the
connection setup is less explicit, and also pollutes global memory.

Because, well:

    $ python -m this | head -n4 | tail -n1

;)
main
Vincent Driessen 13 years ago
parent 31259fa106
commit 48cee215af

@ -1,22 +1,22 @@
import os import os
import time import time
from rq import Queue, use_connection from rq import Queue, Connection
from fib import slow_fib from fib import slow_fib
# Tell RQ what Redis connection to use
use_connection()
# Range of Fibonacci numbers to compute
fib_range = range(20, 34)
# Kick off the tasks asynchronously def main():
async_results = {} # Range of Fibonacci numbers to compute
q = Queue() fib_range = range(20, 34)
for x in fib_range:
# Kick off the tasks asynchronously
async_results = {}
q = Queue()
for x in fib_range:
async_results[x] = q.enqueue(slow_fib, x) async_results[x] = q.enqueue(slow_fib, x)
start_time = time.time() start_time = time.time()
done = False done = False
while not done: while not done:
os.system('clear') os.system('clear')
print 'Asynchronously: (now = %.2f)' % (time.time() - start_time) print 'Asynchronously: (now = %.2f)' % (time.time() - start_time)
done = True done = True
@ -31,4 +31,10 @@ while not done:
print ' python examples/run_worker.py' print ' python examples/run_worker.py'
time.sleep(0.2) time.sleep(0.2)
print 'Done' print 'Done'
if __name__ == '__main__':
# Tell RQ what Redis connection to use
with Connection():
main()

@ -1,8 +1,8 @@
from rq import Queue, Worker, use_connection from rq import Queue, Worker, Connection
# Tell rq what Redis connection to use
use_connection()
if __name__ == '__main__': if __name__ == '__main__':
# Tell rq what Redis connection to use
with Connection():
q = Queue() q = Queue()
Worker(q).work() Worker(q).work()

Loading…
Cancel
Save