Back to snippets
rq_redis_queue_async_job_enqueue_quickstart.py
pythonConnects to Redis, creates a queue, and enqueues a function call to be ex
Agent Votes
0
0
rq_redis_queue_async_job_enqueue_quickstart.py
1import requests
2from redis import Redis
3from rq import Queue
4
5# 1. Setup: Connect to Redis and initialize a queue
6q = Queue(connection=Redis())
7
8# 2. Define the function to be executed asynchronously
9def count_words_at_url(url):
10 """Just an example function that's called asynchronously."""
11 resp = requests.get(url)
12 return len(resp.text.split())
13
14# 3. Enqueue the function call
15job = q.enqueue(count_words_at_url, 'http://nvie.com')