0

I am trying to access a websocket to listen to some information and so I want to use the run_forever command to keep listening until I stop the program, but when I do I get the error below, could someone tell me what I am doing wrong?

Code:

loop = asyncio.get_event_loop() async def listen(): url = "websocket url starting with wss" async with websockets.connect(url) as ws: msg = await ws.recv() print(msg) loop.run_forever(listen()) 

the error then says:

RuntimeWarning: coroutine 'listen' was never awaited loop.run_forever(listen()) RuntimeWarning: Enable tracemalloc to get the object allocation traceback Traceback (most recent call last): File "C:\Users\...", line 18, in <module> loop.run_forever(listen()) TypeError: run_forever() takes 1 positional argument but 2 were given 
1
  • You are running the listen function as soon as giving it as parameter to run_forever. Try run_forever(listen) Commented Jul 22, 2021 at 16:49

1 Answer 1

1

I tried loading your script into Python 3.9.6 and i saw your error.

This is how i got it to run (you need to enter the proper URL):

import asyncio, websockets async def listen(): url = "websocket url starting with wss" async with websockets.connect(url) as ws: msg = await ws.recv() print(msg) async def main(): loop = asyncio.get_event_loop() loop.run_forever(await listen()) if __name__ == "__main__": asyncio.run(main()) 

I hope it can help you to get further with your program.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this was exactly what I was looking for! thank you :)
@kc.chvz You are welcome, feel free to accept my answer. :-)
Ohh got it! so sorry I am new to stackoverflow :D, have a nice day

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.