In python, I have a series of 3 WebSocketApps that all working correctly together and receiving messages, setup as follows:
ws1 = websocket.WebSocketApp("<url1>", on_open=...)
ws1.run_forever(dispatcher=rel, reconnect=5, ping_interval=60, ping_timeout=10)
ws2 = websocket.WebSocketApp("<url2>", on_open=...)
ws2.run_forever(dispatcher=rel, reconnect=5, ping_interval=60, ping_timeout=10)
ws3 = websocket.WebSocketApp("<url3>", on_open=...)
ws3.run_forever(dispatcher=rel, reconnect=5, ping_interval=60, ping_timeout=10)
rel.signal(2, rel.abort)
rel.dispatch()
These all work correctly. However, I would also like to have an ongoing looping function (say, def myFunction: while True: … ) that is also constantly running on its own, sleeping every few seconds. What is the best way to do this?
I tried adding a rel.timeout call to the dispatch to dispatch my own function as well
rel.signal(2, rel.abort)
rel.timeout(1, myFunction) # <---- added
rel.dispatch()
However, I get “RuntimeWarning: coroutine ‘myFunction’ was never awaited” when myFunction is marked async.
When it is not marked async, the myFunction loop takes over and monopolizes the program and none of the websockets receive any messages.
quantumtightening is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.