diff --git a/README.md b/README.md index 9a6be36..22c1c5e 100644 --- a/README.md +++ b/README.md @@ -7,28 +7,36 @@ # 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): if n <= 1: return 1 else: 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 - slow_fib(4) + + # Calculate the 36th Fibonacci number in the background + q = Queue() + q.enqueue(slow_fib, 36) -You can find an example implementation in the `examples/` directory. To run -it, open two terminal windows and run the following commands in them: +If you want to put the work on a specific queue, simply specify its name: -1. `python example/run_worker.py` -1. `python example/run_example.py` + q = Queue('math') + q.enqueue(slow_fib, 36) -This starts two workers and starts crunching the fibonacci calculations in the -background, while the script shows the crunched data updates every second. +You can use any queue name, so you can quite flexibly distribute work to your +own desire. Common patterns are to name your queues after priorities (e.g. +`high`, `medium`, `low`). # Installation 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()