'Roman S' via Tornado Web Server
2018-06-13 12:34:50 UTC
hi all,
I am new to tornado.
*# my application*
here is very simple description
*## websocket server### on_open*
* create tornado queue
* spawn a loop in callback - there messages will be processed by a blocking
function
*### on_close*
* put None to queue to signal an exit from callback loop
*### on message*
* put message to queue
*### loop in callback*
* while True
* get message from queue
* if message is None, then "return" from loop
* if message is text, pass text to blocking function. Once result is
available, "self.write_message(...)"
It works perfect also for concurrent clients.
In one of test cases, client opens connection, sends data and closes
connection. On the server, after the first message was processed, the
callback loop tries to send answer but the connection is already closed.
The "close" event will arrive few ms later...
So I had to wrap "self.write_message(...)" as:
def send_event(self, event):
try:
if self.ws_connection is not None and self.state < DecoderState.
CLOSE:
self.write_message(json.dumps(event), binary=False)
# except tornado.websocket.WebSocketClosedError as e:
except tornado.websocket.WebSocketClosedError:
self.set_state(DecoderState.CLOSE)
print()
print('*** Connection to client is closed')
#print(' |_ %s :: %s' % (type(e), e))
print(' |_ drop event')
pprint.pprint(event)
print()
I try to recognize the closed connection with "set_state()" and drop
message from queue. So everything works well, also cleanups.
BUT, I get error dump on the server (just a print of an error):
ERROR:asyncio:Future exception was never retrieved
future: <Future finished exception=WebSocketClosedError()>
Traceback (most recent call last):
File
"/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/websocket.py"
, line 808, in wrapper
yield fut
File
"/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/gen.py",
line 1099, in run
value = future.result()
File "/usr/lib64/python3.5/asyncio/futures.py", line 294, in result
raise self._exception
tornado.iostream.StreamClosedError: Stream is closed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File
"/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/gen.py",
line 1107, in run
yielded = self.gen.throw(*exc_info)
File
"/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/websocket.py"
, line 810, in wrapper
raise WebSocketClosedError()
tornado.websocket.WebSocketClosedError
What the error stands for? As to my understanding, I have not missed any
message from client, and process each one (even when I have "custom close"
state - my blocking function will ignore messages and not send anything to
client, which has already closed the connection).
I do suspect, that when I got WebSocketClosedError first time, then the
server remembers it...
Thx for any help!
roman
I am new to tornado.
*# my application*
here is very simple description
*## websocket server### on_open*
* create tornado queue
* spawn a loop in callback - there messages will be processed by a blocking
function
*### on_close*
* put None to queue to signal an exit from callback loop
*### on message*
* put message to queue
*### loop in callback*
* while True
* get message from queue
* if message is None, then "return" from loop
* if message is text, pass text to blocking function. Once result is
available, "self.write_message(...)"
It works perfect also for concurrent clients.
In one of test cases, client opens connection, sends data and closes
connection. On the server, after the first message was processed, the
callback loop tries to send answer but the connection is already closed.
The "close" event will arrive few ms later...
So I had to wrap "self.write_message(...)" as:
def send_event(self, event):
try:
if self.ws_connection is not None and self.state < DecoderState.
CLOSE:
self.write_message(json.dumps(event), binary=False)
# except tornado.websocket.WebSocketClosedError as e:
except tornado.websocket.WebSocketClosedError:
self.set_state(DecoderState.CLOSE)
print()
print('*** Connection to client is closed')
#print(' |_ %s :: %s' % (type(e), e))
print(' |_ drop event')
pprint.pprint(event)
print()
I try to recognize the closed connection with "set_state()" and drop
message from queue. So everything works well, also cleanups.
BUT, I get error dump on the server (just a print of an error):
ERROR:asyncio:Future exception was never retrieved
future: <Future finished exception=WebSocketClosedError()>
Traceback (most recent call last):
File
"/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/websocket.py"
, line 808, in wrapper
yield fut
File
"/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/gen.py",
line 1099, in run
value = future.result()
File "/usr/lib64/python3.5/asyncio/futures.py", line 294, in result
raise self._exception
tornado.iostream.StreamClosedError: Stream is closed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File
"/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/gen.py",
line 1107, in run
yielded = self.gen.throw(*exc_info)
File
"/home/madkote/tornado_app/venv/lib/python3.5/site-packages/tornado/websocket.py"
, line 810, in wrapper
raise WebSocketClosedError()
tornado.websocket.WebSocketClosedError
What the error stands for? As to my understanding, I have not missed any
message from client, and process each one (even when I have "custom close"
state - my blocking function will ignore messages and not send anything to
client, which has already closed the connection).
I do suspect, that when I got WebSocketClosedError first time, then the
server remembers it...
Thx for any help!
roman
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.