Issue
I am on python 3.7 with django 2.2.3 running. I want a solution with asyncio so that the api can just call the async function & return the response without waiting just the way we do things with the jquery promises. the definition my_coro
is just for example. I will be running moviepy
functions that usually require 40-50 seconds to complete. I don't want the api to wait that long for sending out the response. I am also confused on how to handle the thread pool. how to use thread pool here? because I intend to make the moviepy iterations fast as well. So how to create a pool for handling my_coro
calls?
async def my_coro(n):
print(f"The answer is {n}.")
async def main():
await asyncio.gather(my_coro(1),my_coro(2),my_coro(3),my_coro(4))
class SaveSnaps(APIView):
def post(self, request, format = None):
if request.user.is_anonymous:
return Response({"response":"FORBIDDEN"}, status = 403)
else:
try:
asyncio.run(main())
return Response({"response": "success"}, status = 200)
except Exception as e:
return Response({'response':str(e)}, status = 400)
Update :
I tried using celery. but as I won't be using periodic tasks & actually the method I need to make asynchronous receives a blob array as parameter. celery's task.delay
is giving me an error because tasks expect serializable params. So I am back to square one on this. I am not sure whether I should stick to the threading solution or something else.
Update : I forgot to share what I did at last. I shifted to celery
. but since celery
's task.delay
expect serialized params
, I moved the blob
saving part to a synchronous method which after completion, hands over the moviepy tasks to the celery
run task.
Solution
As far as I know, asyncio is going to process your code concurrently, but your application is still going to wait for the execution of asyncio to be done, then it continues execution inside your app.
If you want your app to run the code in the background and continue execution, similar to Promises in JS, you need to consider using Job Scheduling using Celery for example, or something similar.
This is a simple Guide for using Django with Celery.
Answered By - Radwan Abu-Odeh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.