Skip to content

How to exit loop running? #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kuifu opened this issue Jun 9, 2018 · 7 comments
Closed

How to exit loop running? #425

kuifu opened this issue Jun 9, 2018 · 7 comments

Comments

@kuifu
Copy link

kuifu commented Jun 9, 2018

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()
@kuifu
Copy link
Author

kuifu commented Jun 9, 2018

Do you have any internal timeout parameter?
If the websocket is dead, how can I detect it and kill it?

async for message in ws:
    print(message)

if websocket is frozen, this code will pause/wait for ever. you can try shutdown you network for 10 seconds and re-enable it.

@aaugustin
Copy link
Member

@kuifu
Copy link
Author

kuifu commented Jun 10, 2018

Thank you for your professional answer.
I helps so much!

@aaugustin
Copy link
Member

I'm not sure if you're sincere or sarcastic :-|

You got the "best effort in my free time" answer and I believe the docs are the best answer I have to your question.

The way you phrased your first question — "How to exit the loop event - run_forever() in my own function?" — suggests it's more a question about asyncio than about websockets.

Your second question — "Do you have any internal timeout parameter?" — is answered by the example I linked to: use asyncio.wait_for.

You can have a more personalized answer in my paid-for time if you'd like. See "Professional support" in the README.

@aaugustin
Copy link
Member

Perhaps this is a duplicate of #414.

@kuifu
Copy link
Author

kuifu commented Jun 11, 2018

I'm sorry to cost your working time. You answer really helped me. It's my fault that I didn't read the docs carefully.

In my mind, you can add more full samples to help others to use websockets.

I just finished my program, can you help to put it on the site? Of course, you can help to review the code.

@aaugustin
Copy link
Member

Thanks for clarifying :-) Sometimes it's hard to tell across the language barrier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants