If anyone could help me with Python and async/await, any help would be much appreciated!
I need to listen to a websocket for messages, so I set up the following code:
import websockets
import asyncio
my_socket = "ws://......."
# I set a "while True" here to reconnect websocket if it stop for any reason
while True:
try:
async with websockets.connect(my_socket) as ws:
# I set a "while True" here to keep listening to messages forever
while True:
await on_message(await ws.recv())
# If websocket gets closed for any reason, we catch exception and wait before new loop
except Exception as e:
print(e)
# Wait 10 secs before new loop to avoid flooding server if it is unavailable for any reason
await asyncio.sleep(10)
async def on_message(message):
# Do what needs to be done with received message
# This function is running for a few minutes, with a lot of sleep() time in it..
# .. so it does no hold process for itself
What I would like to do is:
- Listen to messages
- As soon as a message arrives, apply various actions with
on_message()
function, for several minutes - Keep listening to messages while previous messages are still in process with
on_message()
What actually happens:
- Listen to messages
- Receive a message and start
on_message()
function - And then program is waiting for
on_message()
function to end before receiving any new message, which takes a few minutes, and make the second message late and so on
I do understand why it does this, as await on_message()
clearly says : wait for on_message() to end so it won't go back to listen for new message. The thing I don't know, is how I could handle messages without having to wait for this function to end.
My on_message()
function has a lot of idle time with some await asyncio.sleep(1)
, so I know that I can run multiple task in the same time.
So, how could I keep be listening to new messages while running tasks for the first one?