Skip to content

Commit 99fb0ca

Browse files
committed
Fix asyncio example
1 parent 0dd0bf2 commit 99fb0ca

File tree

2 files changed

+42
-57
lines changed

2 files changed

+42
-57
lines changed

examples/asyncio/asyncio-server.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ def window_updated(self, stream_id, delta):
208208
loop.run_forever()
209209
except KeyboardInterrupt:
210210
pass
211-
212-
# Close the server
213-
server.close()
214-
loop.run_until_complete(server.wait_closed())
215-
loop.close()
211+
finally:
212+
# Close the server
213+
server.close()
214+
loop.run_until_complete(server.wait_closed())
215+
loop.close()

examples/asyncio/wsgi-server.py

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,9 @@ def window_opened(self, event):
193193
for data in self._flow_controlled_data.values():
194194
self._stream_data.put_nowait(data)
195195

196-
self._flow_controlled_data = {}
196+
self._flow_controlled_data.clear()
197197

198-
@asyncio.coroutine
199-
def sending_loop(self):
198+
async def sending_loop(self):
200199
"""
201200
A call that loops forever, attempting to send data. This sending loop
202201
contains most of the flow-control smarts of this class: it pulls data
@@ -216,7 +215,7 @@ def sending_loop(self):
216215
This coroutine explicitly *does not end*.
217216
"""
218217
while True:
219-
stream_id, data, event = yield from self._stream_data.get()
218+
stream_id, data, event = await self._stream_data.get()
220219

221220
# If this stream got reset, just drop the data on the floor. Note
222221
# that we need to reset the event here to make sure that
@@ -327,7 +326,7 @@ def reset_stream(self, event):
327326
data.
328327
"""
329328
if event.stream_id in self._flow_controlled_data:
330-
del self._flow_controlled_data
329+
del self._flow_controlled_data[event.stream_id]
331330

332331
self._reset_streams.add(event.stream_id)
333332
self.end_stream(event)
@@ -534,23 +533,9 @@ def readline(self, hint=None):
534533
def readlines(self, hint=None):
535534
"""
536535
Called by the WSGI application to read several lines of data.
537-
538-
This method is really pretty stupid. It rigorously observes the
539-
``hint`` parameter, and quite happily returns the input split into
540-
lines.
541536
"""
542-
# This method is *crazy inefficient*, but it's also a pretty stupid
543-
# method to call.
544537
data = self.read(hint)
545-
lines = data.split(b'\n')
546-
547-
# Split removes the newline character, but we want it, so put it back.
548-
lines = [line + b'\n' for line in lines]
549-
550-
# Except if the last character was a newline character we now have an
551-
# extra line that is just a newline: pull that out.
552-
if lines[-1] == b'\n':
553-
lines = lines[:-1]
538+
lines = data.splitlines(True)
554539
return lines
555540

556541
def start_response(self, status, response_headers, exc_info=None):
@@ -688,41 +673,41 @@ def _build_environ_dict(headers, stream):
688673
version you'd want to fix it.
689674
"""
690675
header_dict = dict(headers)
691-
path = header_dict.pop(u':path')
676+
path = header_dict.pop(':path')
692677
try:
693-
path, query = path.split(u'?', 1)
678+
path, query = path.split('?', 1)
694679
except ValueError:
695-
query = u""
696-
server_name = header_dict.pop(u':authority')
680+
query = ""
681+
server_name = header_dict.pop(':authority')
697682
try:
698-
server_name, port = server_name.split(u':', 1)
699-
except ValueError as e:
683+
server_name, port = server_name.split(':', 1)
684+
except ValueError:
700685
port = "8443"
701686

702687
environ = {
703-
u'REQUEST_METHOD': header_dict.pop(u':method'),
704-
u'SCRIPT_NAME': u'',
705-
u'PATH_INFO': path,
706-
u'QUERY_STRING': query,
707-
u'SERVER_NAME': server_name,
708-
u'SERVER_PORT': port,
709-
u'SERVER_PROTOCOL': u'HTTP/2',
710-
u'HTTPS': u"on",
711-
u'SSL_PROTOCOL': u'TLSv1.2',
712-
u'wsgi.version': (1, 0),
713-
u'wsgi.url_scheme': header_dict.pop(u':scheme'),
714-
u'wsgi.input': stream,
715-
u'wsgi.errors': sys.stderr,
716-
u'wsgi.multithread': True,
717-
u'wsgi.multiprocess': False,
718-
u'wsgi.run_once': False,
688+
'REQUEST_METHOD': header_dict.pop(':method'),
689+
'SCRIPT_NAME': '',
690+
'PATH_INFO': path,
691+
'QUERY_STRING': query,
692+
'SERVER_NAME': server_name,
693+
'SERVER_PORT': port,
694+
'SERVER_PROTOCOL': 'HTTP/2',
695+
'HTTPS': "on",
696+
'SSL_PROTOCOL': 'TLSv1.2',
697+
'wsgi.version': (1, 0),
698+
'wsgi.url_scheme': header_dict.pop(':scheme'),
699+
'wsgi.input': stream,
700+
'wsgi.errors': sys.stderr,
701+
'wsgi.multithread': True,
702+
'wsgi.multiprocess': False,
703+
'wsgi.run_once': False,
719704
}
720-
if u'content-type' in header_dict:
721-
environ[u'CONTENT_TYPE'] = header_dict[u'content-type']
722-
if u'content-length' in header_dict:
723-
environ[u'CONTENT_LENGTH'] = header_dict[u'content-length']
705+
if 'content-type' in header_dict:
706+
environ['CONTENT_TYPE'] = header_dict.pop('content-type')
707+
if 'content-length' in header_dict:
708+
environ['CONTENT_LENGTH'] = header_dict.pop('content-length')
724709
for name, value in header_dict.items():
725-
environ[u'HTTP_' + name.upper()] = value
710+
environ['HTTP_' + name.upper()] = value
726711
return environ
727712

728713

@@ -753,8 +738,8 @@ def _build_environ_dict(headers, stream):
753738
loop.run_forever()
754739
except KeyboardInterrupt:
755740
pass
756-
757-
# Close the server
758-
server.close()
759-
loop.run_until_complete(server.wait_closed())
760-
loop.close()
741+
finally:
742+
# Close the server
743+
server.close()
744+
loop.run_until_complete(server.wait_closed())
745+
loop.close()

0 commit comments

Comments
 (0)