Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

How to give ws.receive() a timeout value and default value? #76

Open
tme-wl opened this issue Aug 23, 2020 · 2 comments
Open

How to give ws.receive() a timeout value and default value? #76

tme-wl opened this issue Aug 23, 2020 · 2 comments

Comments

@tme-wl
Copy link

tme-wl commented Aug 23, 2020

I mean

msg = ws.receive(timeout=1, default='bob')

if client no send data, after 1 second , give a default value to msg

how to do like this? please!!!!!

@dylancrockett
Copy link

dylancrockett commented Aug 25, 2020

I am not sure why you would want to do this but I do believe gevent provides a workaround for situations like this.

For example you could do the following to achieve your timeout of 1 second:

msg = None  # be aware that None is also returned when the websocket connection has closed

# give the code within the with statement 1 second to run before it is forced to stop
with Timeout(1, False):
    msg = ws.receive()

# check if we timed out without receiving a value
if msg is None:
    msg = "bob"
# continue with your code

If you could explain why you want to do this I might be able to offer some better solutions for what you want to do.

@tme-wl
Copy link
Author

tme-wl commented Sep 27, 2020

our web client send msg not a regular time, but our web server must send data every 1 second. so, if I receive it, I can't send data every 1 second , if I do not receive it , I can't catch the msg.

so now , I used 2 threading to solve this,one receive msg one send data every 1 second. bug it is not pythonic.

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

No branches or pull requests

2 participants