Asynchronous call to RASA webhook API

I have a synchronous function, Let’s say - def func(): #inside this function I want to call an async function which updates the conversations on RASA X, I don’t want to await the response from the webhook API.

After making call to the async function, I want this sync function(func() here) to continue execution of lines following it.

I read about asyncio.create_task, it’s makes a non-blocking call. But the problem is I want to create_task inside a synchronous function which gives the following error- RuntimeWarning: coroutine ‘xyz’ was never awaited.

i am stuck on this problem for quite sometime. Please help. Thanks in advance

Hey @arushi, this is really a question about Python and not about Rasa, so I recommend to look for an answer on StackOverflow or similar :slightly_smiling_face: However, as someone also pretty new to this area, do I understand it correctly that you don’t want to wait for the result of the call at all, and your synchronous function can even return before that call finishes? In particular, what if the call goes wrong? How do you find out, so that you can re-try sending the conversations to Rasa X?

Hey @SamS, you are absolutely correct that this question is more on Python than RASA. I went through some answers on StackOverflow, but things didn’t seem to work the expected way. So I thought of seeking some help here.

So basically for now I am using NLU feature of RASA. I have RASA X hosted on a server, so my python code fetches a response(Intent matched) for a particular text from there. But before I do this, I want to ‘POST’ to webhook API(this task could be running in the background while my synchronous function performs the rest of the stuff required).

Hmmm, well, my intuition is that you can’t easily mix sync and async tasks. I.e. after creating an async one, you can’t easily let it run and move on to some sync tasks. But if you have a bunch of async tasks, then you can create them and let them all run like in this example. Maybe this helps?

consider using a background task manager for such thing where you want to fire and forget, while async/await is great to allow other tasks to execute mostly iops, i dont know if this is the correct usage of async/await where you have series of tasks to execute in a sync endpoint, you would consider a task manager like celery where you can submit such tasks. Celery is used by popular frameworks like Django/Flask to execute tasks in such a fashion where you dont await a response because they dont have async implemented

I use FastAPI and they do have an implementation called BackgroundTasks that fires requests and doesn’t wait for response. but indeed it is built for smaller operations.

1 Like

Thanks @SamS, I’ll look into it if somehow this could be of help.

Hey @souvikg10 , I looked into FastAPI - but I don’t think I can use this framework as it’s asynchronous. Celery could be an option, but the task is small(just updating conversation on Rasa x) so thought of not using it.

I am just trying to find out if there could be a better and simpler way of doing it. Thanks for responding.

Well perhaps threading might work for you https://medium.com/@hassansajedi/running-a-method-as-a-background-process-in-python-adc4c7407543

though i am not 100% sure if the thread forked is going to use the same pid and thus you might have the infamous GIL. Another option will be to fork a process and execute a python script that runs a separate interpreter.