Update README (and example).

main
Vincent Driessen 13 years ago
parent 1a893e60cf
commit 22f3da1832

@ -7,28 +7,36 @@
# Putting jobs on queues # Putting jobs on queues
To put jobs on queues, first declare a Python function call as a job, like so: To put jobs on queues, first declare a Python function to be called on
a background process:
@job('default')
def slow_fib(n): def slow_fib(n):
if n <= 1: if n <= 1:
return 1 return 1
else: else:
return slow_fib(n-1) + slow_fib(n-2) return slow_fib(n-1) + slow_fib(n-2)
You can still call the function synchronously: Notice anything? There's nothing special about a job! Any Python function can
be put on an RQ queue, as long as the function is in a module that is
accessible from the worker process.
To calculate the 36th Fibonacci number in the background, simply do this:
from rq import Queue
from fib import slow_fib from fib import slow_fib
slow_fib(4)
You can find an example implementation in the `examples/` directory. To run # Calculate the 36th Fibonacci number in the background
it, open two terminal windows and run the following commands in them: q = Queue()
q.enqueue(slow_fib, 36)
If you want to put the work on a specific queue, simply specify its name:
1. `python example/run_worker.py` q = Queue('math')
1. `python example/run_example.py` q.enqueue(slow_fib, 36)
This starts two workers and starts crunching the fibonacci calculations in the You can use any queue name, so you can quite flexibly distribute work to your
background, while the script shows the crunched data updates every second. own desire. Common patterns are to name your queues after priorities (e.g.
`high`, `medium`, `low`).
# Installation # Installation

@ -1,6 +1,3 @@
from rq import job
@job('default')
def slow_fib(n): def slow_fib(n):
if n <= 1: if n <= 1:
return 1 return 1

@ -1,23 +1,29 @@
import os import os
import time import time
from rq import conn from rq import Queue
from redis import Redis
from fib import slow_fib 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()) conn.push(Redis())
# Range of Fibonacci numbers to compute
fib_range = range(20, 34)
# Kick off the tasks asynchronously # Kick off the tasks asynchronously
async_results = {} async_results = {}
for x in range(20, 30): q = Queue()
async_results[x] = slow_fib.delay(x) for x in fib_range:
async_results[x] = q.enqueue(slow_fib, x)
start_time = time.time()
done = False done = False
while not done: while not done:
os.system('clear') os.system('clear')
print 'Asynchronously: (now = %s)' % time.time() print 'Asynchronously: (now = %.2f)' % (time.time() - start_time)
done = True done = True
for x in range(20, 30): for x in fib_range:
result = async_results[x].return_value result = async_results[x].return_value
if result is None: if result is None:
done = False done = False
@ -26,6 +32,6 @@ while not done:
print '' print ''
print 'To start the actual in the background, run a worker:' print 'To start the actual in the background, run a worker:'
print ' python examples/run_worker.py' print ' python examples/run_worker.py'
time.sleep(1) time.sleep(0.2)
print 'Done' print 'Done'

@ -1,9 +1,10 @@
from redis import Redis from rq import Queue, Worker
from rq import conn
from rq.daemon import run_daemon
# 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()) conn.push(Redis())
listen_on_queues = ['default'] if __name__ == '__main__':
run_daemon(listen_on_queues) q = Queue()
Worker(q).work_forever()

Loading…
Cancel
Save