The examples I've found wait for 2 fd's with a timeout. The other one is
redis in these examples.
My wait_for_new_data() function waits for a gevent.event.Event to be set,
not for redis, so I can't see how gevent.select or uwsgi.wait_fd can help
here.
This is the code I ended up with.
Note: While inspecting tcpdump output, it seems uwsgi wakes up by itself
every 60 seconds, and the line "gevent.socket.wait_read(websocket_fd)"
unblocks.
* Is there anything wrong* with this code? Thanks for you time, guys
uwsgi.websocket_handshake(...)
ready = gevent.event.Event()
dead = gevent.event.Event()
def ws_socket_waiter():
while not dead.is_set():
gevent.socket.wait_read(websocket_fd)
ready.set()
def data_ready_waiter():
while not dead.is_set():
self.data_available_event.wait()
ready.set()
gevent.spawn(ws_socket_waiter)
gevent.spawn(data_ready_waiter)
try:
while True:
ready.wait()
msg = uwsgi.websocket_recv_nb()
if msg:
logging.info('Received msg %s', msg)
continue
events = get_data_noblock_if_available()
if events:
logging.info('sending events')
# Is this line needed?
gevent.socket.wait_write(websocket_fd)
uwsgi.websocket_send(json.dumps({'updates': events}))
ready.clear()
except IOError:
pass
finally:
dead.set()
On Thu, Feb 27, 2014 at 3:27 PM, Igor Katson <[email protected]> wrote:
> Hi,
>
> I'm trying to use websockets with gevent, and a bit stuck with
> non-blocking api.
> I don't need to read from websocket for now, but rather only send events
> to the browser, when there is new information on the server.
>
> I tried doing it first with greenlets, e.g. something like
>
> reader_dead = gevent.Event()
>
> def reader():
> try:
> while True:
> msg = uwsgi.websocket_recv()
> if msg:
> logging.info('Received %s', msg)
> except:
> reader_dead.set()
> raise
>
> def writer():
> while not reader_dead.is_set():
> data = wait_for_new_data(timeout=None)
> uwsgi.websocket_send(data)
>
> gevent.spawn(writer)
> gevent.spawn(reader).join()
>
>
> but uwsgi complains, that the API functions should be called from the main
> executable.
> Should I write the same code in non-blocking style? How can I do that? I
> don't understand non-blocking well enough to write this quickly
>
> Thanks!
>
_______________________________________________
uWSGI mailing list
[email protected]
http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi