提交 568897d2 编写于 作者: B Ben Darnell

concurrent: Deprecate callback argument with run_on_executor

上级 f6f56f58
...@@ -426,6 +426,12 @@ def run_on_executor(*args, **kwargs): ...@@ -426,6 +426,12 @@ def run_on_executor(*args, **kwargs):
.. versionchanged:: 5.1 .. versionchanged:: 5.1
Returns a `.Future` compatible with ``await`` instead of a Returns a `.Future` compatible with ``await`` instead of a
`concurrent.futures.Future`. `concurrent.futures.Future`.
.. deprecated:: 5.1
The ``callback`` argument is deprecated and will be removed in
6.0. The decorator itself is discouraged in new code but will
not be removed in 6.0.
""" """
def run_on_executor_decorator(fn): def run_on_executor_decorator(fn):
executor = kwargs.get("executor", "executor") executor = kwargs.get("executor", "executor")
...@@ -437,6 +443,8 @@ def run_on_executor(*args, **kwargs): ...@@ -437,6 +443,8 @@ def run_on_executor(*args, **kwargs):
conc_future = getattr(self, executor).submit(fn, self, *args, **kwargs) conc_future = getattr(self, executor).submit(fn, self, *args, **kwargs)
chain_future(conc_future, async_future) chain_future(conc_future, async_future)
if callback: if callback:
warnings.warn("callback arguments are deprecated, use the returned Future instead",
DeprecationWarning)
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
IOLoop.current().add_future( IOLoop.current().add_future(
async_future, lambda future: callback(future.result())) async_future, lambda future: callback(future.result()))
......
...@@ -328,6 +328,10 @@ class Resolver(Configurable): ...@@ -328,6 +328,10 @@ class Resolver(Configurable):
.. versionchanged:: 4.4 .. versionchanged:: 4.4
Standardized all implementations to raise `IOError`. Standardized all implementations to raise `IOError`.
.. deprecated:: 5.1
The ``callback`` argument is deprecated and will be removed in 6.0.
Use the returned awaitable object instead.
""" """
raise NotImplementedError() raise NotImplementedError()
......
...@@ -13,7 +13,7 @@ from tornado.netutil import ( ...@@ -13,7 +13,7 @@ from tornado.netutil import (
) )
from tornado.stack_context import ExceptionStackContext from tornado.stack_context import ExceptionStackContext
from tornado.testing import AsyncTestCase, gen_test, bind_unused_port from tornado.testing import AsyncTestCase, gen_test, bind_unused_port
from tornado.test.util import unittest, skipIfNoNetwork from tornado.test.util import unittest, skipIfNoNetwork, ignore_deprecation
try: try:
from concurrent import futures from concurrent import futures
...@@ -38,7 +38,8 @@ else: ...@@ -38,7 +38,8 @@ else:
class _ResolverTestMixin(object): class _ResolverTestMixin(object):
def test_localhost(self): def test_localhost(self):
self.resolver.resolve('localhost', 80, callback=self.stop) with ignore_deprecation():
self.resolver.resolve('localhost', 80, callback=self.stop)
result = self.wait() result = self.wait()
self.assertIn((socket.AF_INET, ('127.0.0.1', 80)), result) self.assertIn((socket.AF_INET, ('127.0.0.1', 80)), result)
...@@ -59,7 +60,8 @@ class _ResolverErrorTestMixin(object): ...@@ -59,7 +60,8 @@ class _ResolverErrorTestMixin(object):
return True # Halt propagation. return True # Halt propagation.
with ExceptionStackContext(handler): with ExceptionStackContext(handler):
self.resolver.resolve('an invalid domain', 80, callback=self.stop) with ignore_deprecation():
self.resolver.resolve('an invalid domain', 80, callback=self.stop)
result = self.wait() result = self.wait()
self.assertIsInstance(result, Exception) self.assertIsInstance(result, Exception)
...@@ -108,13 +110,12 @@ class OverrideResolverTest(AsyncTestCase, _ResolverTestMixin): ...@@ -108,13 +110,12 @@ class OverrideResolverTest(AsyncTestCase, _ResolverTestMixin):
} }
self.resolver = OverrideResolver(BlockingResolver(), mapping) self.resolver = OverrideResolver(BlockingResolver(), mapping)
@gen_test
def test_resolve_multiaddr(self): def test_resolve_multiaddr(self):
self.resolver.resolve('google.com', 80, socket.AF_INET, callback=self.stop) result = yield self.resolver.resolve('google.com', 80, socket.AF_INET)
result = self.wait()
self.assertIn((socket.AF_INET, ('1.2.3.4', 80)), result) self.assertIn((socket.AF_INET, ('1.2.3.4', 80)), result)
self.resolver.resolve('google.com', 80, socket.AF_INET6, callback=self.stop) result = yield self.resolver.resolve('google.com', 80, socket.AF_INET6)
result = self.wait()
self.assertIn((socket.AF_INET6, ('2a02:6b8:7c:40c:c51e:495f:e23a:3', 80, 0, 0)), result) self.assertIn((socket.AF_INET6, ('2a02:6b8:7c:40c:c51e:495f:e23a:3', 80, 0, 0)), result)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册