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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-19 04:39:01 +0000
Seen: 19 times
Last updated: Jun 19 '23
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?