파이썬
라이브러리
RQ
#
Find similar titles
- (rev. 2)
- Kyooyeol Lee
Structured data
- Category
- Programming
- Algorithm
- Database
Simple job queues for Python #
RQ (Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers. It is backed by Redis and it is designed to have a low barrier to entry. It can be integrated in your web stack easily. If there is a need to run background tasks in your web application, then it can be achieved easily by Python module called 'rq'. It is simple to use and implement. As it is based on Redis, it requires Redis connection.
How to install #
Installation can be done easily by pip package manager:
$ pip install rq
If you want to install latest development version:
$ pip install -e git+git@github.com:nvie/rq.git@master#egg=rq
Example #
First, we need to run Redis server in order to start our job queue service.
$ redis-server --loglevel verbose
17503:M 18 Jan 16:35:20.830 * Increased maximum number of open files to 10032 (it was originally set to 2560).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 17503
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
17503:M 18 Jan 16:35:20.838 # Server started, Redis version 3.0.6
17503:M 18 Jan 16:35:20.838 * The server is now ready to accept connections on port 6379
17503:M 18 Jan 16:35:21.839 - 0 clients connected (0 slaves), 957936 bytes in use
Write some simple blocking function:
import requests
def count_words_at_url(url):
resp = requests.get(url)
return len(resp.text.split())
Create RQ queue:
from redis import Redis
from rq import Queue
q = Queue(connection=Redis())
Enqueue function call:
from my_module import count_words_at_url
result = q.enqueue(
count_words_at_url, 'http://incodom.kr')
Worker #
RQ has worker which is based on Redis module. After installation worker can be easily started as:
$ rqworker
*** Listening for work on default
Got count_words_at_url('http://nvie.com') from default
Job result = 818
*** Listening for work on default