1 | initial version |
You can run an awaitable/async function in Python RQ using the asyncio
library. Here's how to do it:
async=True
parameter:from rq import Worker
from rq import Queue
from redis import Redis
redis_conn = Redis()
queue = Queue('default', connection=redis_conn)
worker = Worker([queue], connection=redis_conn, async=True)
async def
keyword:async def my_async_function():
# do some async task
return 'async task done'
asyncio
:import asyncio
from rq.job import Job
async def add_async_job_to_queue():
job = await asyncio.create_task(queue.enqueue(my_async_function))
return job
This will create an async job in RQ queue and return its job ID. You can use this ID to check the status of the job or to get its result.
worker.work()
This will start the worker and it will execute the async jobs in the queue. You can stop the worker by pressing Ctrl+C.
Note: Keep in mind that you need to have an event loop running in your code to use async/await
syntax.