Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Add Tornado 5.x support #328

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
]

tornado_requires = [
"tornado>=4.0,<5.0",
"toro>=0.6"
"tornado>=4.0,<6.0",
]

try:
from tornado import version as tornado_version
if tornado_version < '5.0':
tornado_requires.append("toro>=0.6")
except ImportError:
# tornado will now only get installed and we'll get the newer one
pass

dev_requires = [
"cython>=0.23",
"flake8>=2.5",
Expand Down
24 changes: 17 additions & 7 deletions tests/test_framed_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from thriftpy.transport.framed import TFramedTransportFactory
from thriftpy.protocol.binary import TBinaryProtocolFactory

try:
import asyncio
except ImportError:
asyncio = None

from thriftpy._compat import CYTHON
logging.basicConfig(level=logging.INFO)

Expand Down Expand Up @@ -53,14 +58,9 @@ class FramedTransportTestCase(TestCase):
PROTOCOL_FACTORY = TBinaryProtocolFactory()

def mk_server(self):
self.io_loop = ioloop.IOLoop()
server = make_server(addressbook.AddressBookService,
Dispatcher(self.io_loop), io_loop=self.io_loop)

self.server = server
sock = self.server_sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
sock.bind(('localhost', 0))
sock.bind(('127.0.0.1', 0))
sock.setblocking(0)
self.port = sock.getsockname()[-1]
self.server_thread = threading.Thread(target=self.listen)
Expand All @@ -69,7 +69,14 @@ def mk_server(self):

def listen(self):
self.server_sock.listen(128)
self.server.add_socket(self.server_sock)
if asyncio:
# In Tornado 5.0+, the asyncio event loop will be used
# automatically by default
asyncio.set_event_loop(asyncio.new_event_loop())
self.io_loop = ioloop.IOLoop.current()
server = make_server(addressbook.AddressBookService,
Dispatcher(self.io_loop), io_loop=self.io_loop)
server.add_socket(self.server_sock)
self.io_loop.start()

def mk_client(self):
Expand All @@ -83,6 +90,9 @@ def setUp(self):
time.sleep(0.1)
self.client = self.mk_client()

def tearDown(self):
self.io_loop.stop()

def test_able_to_communicate(self):
dennis = addressbook.Person(name='Dennis Ritchie')
success = self.client.add(dennis)
Expand Down
41 changes: 31 additions & 10 deletions thriftpy/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from __future__ import absolute_import

from contextlib import contextmanager
from tornado import tcpserver, ioloop, iostream, gen
from tornado import tcpserver, iostream, gen
from tornado import version as tornado_version
from io import BytesIO
from datetime import timedelta

Expand All @@ -32,7 +33,15 @@
import logging
import socket
import struct
import toro

try:
from tornado.locks import Lock
except ImportError:
try:
from toro import Lock
except ImportError:
raise RuntimeError('With tornado {}, you need to install '
'"toro"'.format(tornado_version))


logger = logging.getLogger(__name__)
Expand All @@ -47,21 +56,25 @@ def __init__(self, host, port, stream=None, io_loop=None, ssl_options=None,
read_timeout=DEFAULT_READ_TIMEOUT):
self.host = host
self.port = port
self.io_loop = io_loop or ioloop.IOLoop.current()
self.io_loop = io_loop
self.read_timeout = read_timeout
self.is_queuing_reads = False
self.read_queue = []
self.__wbuf = BytesIO()
self._read_lock = toro.Lock()
self._read_lock = Lock()
self.ssl_options = ssl_options

# servers provide a ready-to-go stream
self.stream = stream
if self.stream is not None:
self._set_close_callback()

def with_timeout(self, timeout, future):
return gen.with_timeout(timeout, future, self.io_loop)
if tornado_version >= '5.0':
def with_timeout(self, timeout, future):
return gen.with_timeout(timeout, future)
else:
def with_timeout(self, timeout, future):
return gen.with_timeout(timeout, future, self.io_loop)

@gen.coroutine
def open(self, timeout=DEFAULT_CONNECT_TIMEOUT):
Expand Down Expand Up @@ -158,12 +171,15 @@ def __init__(self, processor, iprot_factory, oprot_factory=None,
else iprot_factory)
self.transport_read_timeout = transport_read_timeout

# `io_loop` has been deprecated since tornado 4.1 and removed in 5.0
self.__io_loop = getattr(self, 'io_loop', None)

@gen.coroutine
def handle_stream(self, stream, address):
host, port = address
trans = TTornadoStreamTransport(
host=host, port=port, stream=stream,
io_loop=self.io_loop, read_timeout=self.transport_read_timeout)
io_loop=self.__io_loop, read_timeout=self.transport_read_timeout)
try:
oprot = self._oprot_factory.get_protocol(trans)
iprot = self._iprot_factory.get_protocol(TMemoryBuffer())
Expand Down Expand Up @@ -214,9 +230,14 @@ def make_server(
io_loop=None, ssl_options=None,
transport_read_timeout=TTornadoStreamTransport.DEFAULT_READ_TIMEOUT):
processor = TProcessor(service, handler)
server = TTornadoServer(processor, iprot_factory=proto_factory,
transport_read_timeout=transport_read_timeout,
io_loop=io_loop, ssl_options=ssl_options)
if tornado_version >= '5.0':
server = TTornadoServer(processor, iprot_factory=proto_factory,
transport_read_timeout=transport_read_timeout,
ssl_options=ssl_options)
else:
server = TTornadoServer(processor, iprot_factory=proto_factory,
transport_read_timeout=transport_read_timeout,
io_loop=io_loop, ssl_options=ssl_options)
return server


Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ commands =

deps =
pytest
tornado
toro
tornado>=4.0,<6.0
cython

[testenv:flake8]
Expand Down