mirror of https://github.com/rq/rq.git
Update README (and example).
This commit is contained in:
parent
1a893e60cf
commit
22f3da1832
28
README.md
28
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)
|
||||
|
||||
You can find an example implementation in the `examples/` directory. To run
|
||||
it, open two terminal windows and run the following commands in them:
|
||||
# Calculate the 36th Fibonacci number in the background
|
||||
q = Queue()
|
||||
q.enqueue(slow_fib, 36)
|
||||
|
||||
1. `python example/run_worker.py`
|
||||
1. `python example/run_example.py`
|
||||
If you want to put the work on a specific queue, simply specify its name:
|
||||
|
||||
This starts two workers and starts crunching the fibonacci calculations in the
|
||||
background, while the script shows the crunched data updates every second.
|
||||
q = Queue('math')
|
||||
q.enqueue(slow_fib, 36)
|
||||
|
||||
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
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
from rq import job
|
||||
|
||||
@job('default')
|
||||
def slow_fib(n):
|
||||
if n <= 1:
|
||||
return 1
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue