Skip to content

How to exit loop running? #425

@kuifu

Description

@kuifu

I'm running websocket client in my MacOS. Everything is OK after running.
Then I shutdown the wifi connection for 10 seconds, and re-connect to wifi .
I found that my application is dead....

So, How to exit the loop event - run_forever() in my own function? thanks.

class WSS():
    WSS.wss_uri= 'wss://...'

    async def wss_main():
        async with websockets.connect(WSS.wss_uri)  as ws:
            async for message in ws:
                print(message)
                # If the network has some issue , I want to quit this loop. How to ?

def wss_run():
    running = False
    while True:
        if not running:
            try:
                running = True
                wss = WSS()
                asyncio.get_event_loop().run_until_complete(wss.wss_main())
                asyncio.get_event_loop().run_forever()
            except Exception as e:
                print(e)
                running = False
        time.sleep(1)

wss_run()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions