From ad19d649116074cad2550b0c198fe5d69636959f Mon Sep 17 00:00:00 2001 From: Andrew Rabert Date: Thu, 13 Dec 2018 17:38:19 -0500 Subject: [PATCH] Use native async Also remove a test that isn't that useful --- README.rst | 7 +++---- examples/statsd.py | 8 ++++---- sprockets/mixins/metrics/statsd.py | 8 ++++---- sprockets/mixins/metrics/testing.py | 7 +++---- tests.py | 20 ++++---------------- 5 files changed, 18 insertions(+), 32 deletions(-) diff --git a/README.rst b/README.rst index c62cf6f..f8a2a46 100644 --- a/README.rst +++ b/README.rst @@ -26,7 +26,7 @@ call to the ``get`` method as well as a separate metric for the database query. from sprockets.mixins import mediatype from sprockets.mixins.metrics import statsd - from tornado import gen, web + from tornado import web import queries def make_application(): @@ -47,10 +47,9 @@ call to the ``get`` method as well as a separate metric for the database query. super(MyHandler, self).initialize() self.db = queries.TornadoSession(os.environ['MY_PGSQL_DSN']) - @gen.coroutine - def get(self, obj_id): + async def get(self, obj_id): with self.execution_timer('dbquery', 'get'): - result = yield self.db.query('SELECT * FROM foo WHERE id=%s', + result = await self.db.query('SELECT * FROM foo WHERE id=%s', obj_id) self.send_response(result) diff --git a/examples/statsd.py b/examples/statsd.py index 27cca9a..6e0413f 100644 --- a/examples/statsd.py +++ b/examples/statsd.py @@ -1,7 +1,8 @@ +import asyncio import signal from sprockets.mixins.metrics import statsd -from tornado import concurrent, gen, ioloop, web +from tornado import ioloop, web class SimpleHandler(statsd.StatsdMixin, web.RequestHandler): @@ -14,9 +15,8 @@ class SimpleHandler(statsd.StatsdMixin, web.RequestHandler): """ - @gen.coroutine - def get(self): - yield gen.sleep(0.25) + async def get(self): + await asyncio.sleep(0.25) self.set_status(204) self.finish() diff --git a/sprockets/mixins/metrics/statsd.py b/sprockets/mixins/metrics/statsd.py index 96ed44a..78308bf 100644 --- a/sprockets/mixins/metrics/statsd.py +++ b/sprockets/mixins/metrics/statsd.py @@ -1,10 +1,11 @@ +import asyncio import contextlib import logging import os import socket import time -from tornado import gen, iostream +from tornado import iostream LOGGER = logging.getLogger(__name__) @@ -134,12 +135,11 @@ class StatsDCollector: sock.set_close_callback(self._tcp_on_closed) return sock - @gen.engine - def _tcp_on_closed(self): + async def _tcp_on_closed(self): """Invoked when the socket is closed.""" LOGGER.warning('Not connected to statsd, connecting in %s seconds', self._tcp_reconnect_sleep) - yield gen.sleep(self._tcp_reconnect_sleep) + await asyncio.sleep(self._tcp_reconnect_sleep) self._sock = self._tcp_socket() def _tcp_on_connected(self): diff --git a/sprockets/mixins/metrics/testing.py b/sprockets/mixins/metrics/testing.py index 465738b..3155fe8 100644 --- a/sprockets/mixins/metrics/testing.py +++ b/sprockets/mixins/metrics/testing.py @@ -2,7 +2,7 @@ import logging import re import socket -from tornado import gen, iostream, locks, tcpserver, testing +from tornado import iostream, locks, tcpserver, testing LOGGER = logging.getLogger(__name__) @@ -67,11 +67,10 @@ class FakeStatsdServer(tcpserver.TCPServer): self.socket.close() self.socket = None - @gen.coroutine - def handle_stream(self, stream, address): + async def handle_stream(self, stream, address): while True: try: - result = yield stream.read_until_regex(self.TCP_PATTERN) + result = await stream.read_until_regex(self.TCP_PATTERN) except iostream.StreamClosedError: break else: diff --git a/tests.py b/tests.py index 7cfef5b..2f90665 100644 --- a/tests.py +++ b/tests.py @@ -1,9 +1,10 @@ +import asyncio import itertools import socket import unittest from unittest import mock -from tornado import gen, iostream, testing, web +from tornado import iostream, testing, web from sprockets.mixins.metrics import statsd from sprockets.mixins.metrics.testing import FakeStatsdServer @@ -12,10 +13,9 @@ import examples.statsd class CounterBumper(statsd.StatsdMixin, web.RequestHandler): - @gen.coroutine - def get(self, counter, value): + async def get(self, counter, value): with self.execution_timer(*counter.split('.')): - yield gen.sleep(float(value)) + await asyncio.sleep(float(value)) self.set_status(204) self.finish() @@ -75,18 +75,6 @@ class TCPStatsdMetricCollectionTests(testing.AsyncHTTPTestCase): 'protocol': 'tcp', 'prepend_metric_type': True}) - def test_tcp_reconnect_on_stream_close(self): - path_sleep = 'tornado.gen.sleep' - path_statsd = self.application.statsd - with mock.patch(path_sleep) as gen_sleep, \ - mock.patch.object(path_statsd, '_tcp_socket') as mock_tcp_socket: - f = web.Future() - f.set_result(None) - gen_sleep.return_value = f - - self.application.statsd._tcp_on_closed() - mock_tcp_socket.assert_called_once_with() - @mock.patch.object(iostream.IOStream, 'write') def test_write_not_executed_when_connection_is_closed(self, mock_write): self.application.statsd._sock.close()