[Python-checkins] cpython: asyncio: Pass cancellation from wrapping Future to wrapped Future. By Saúl
guido.van.rossum
python-checkins at python.org
Fri Nov 22 20:49:15 CET 2013
http://hg.python.org/cpython/rev/47f5c86d32ea
changeset: 87359:47f5c86d32ea
user: Guido van Rossum <guido at dropbox.com>
date: Fri Nov 22 11:47:22 2013 -0800
summary:
asyncio: Pass cancellation from wrapping Future to wrapped Future. By Saúl Ibarra Corretgé (mostly).
files:
Doc/library/asyncio.rst | 20 +++++++++++++++
Doc/library/concurrency.rst | 1 +
Lib/asyncio/futures.py | 11 ++++++--
Lib/test/test_asyncio/test_futures.py | 18 +++++++++++++
4 files changed, 47 insertions(+), 3 deletions(-)
diff --git a/Doc/library/asyncio.rst b/Doc/library/asyncio.rst
new file mode 100644
--- /dev/null
+++ b/Doc/library/asyncio.rst
@@ -0,0 +1,20 @@
+:mod:`asyncio` -- Asynchronous I/O, event loop, coroutines and tasks
+====================================================================
+
+.. module:: asyncio
+ :synopsis: Asynchronous I/O, event loop, coroutines and tasks.
+
+.. versionadded:: 3.4
+
+
+Introduction
+------------
+
+This package includes a pluggable event loop, transport and protocol
+abstractions similar to those in Twisted, and a higher-level scheduler
+for coroutines and tasks based on ``yield from`` (:PEP:`380`).
+
+Full documentation is not yet ready; we hope to have it written
+before Python 3.4 leaves beta. Until then, the best reference is
+:PEP:`3156`. For a motivational primer on transports and protocols,
+see :PEP:`3153`.
diff --git a/Doc/library/concurrency.rst b/Doc/library/concurrency.rst
--- a/Doc/library/concurrency.rst
+++ b/Doc/library/concurrency.rst
@@ -22,6 +22,7 @@
queue.rst
select.rst
selectors.rst
+ asyncio.rst
The following are support modules for some of the above services:
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py
--- a/Lib/asyncio/futures.py
+++ b/Lib/asyncio/futures.py
@@ -301,6 +301,8 @@
The other Future may be a concurrent.futures.Future.
"""
assert other.done()
+ if self.cancelled():
+ return
assert not self.done()
if other.cancelled():
self.cancel()
@@ -324,14 +326,17 @@
"""Wrap concurrent.futures.Future object."""
if isinstance(fut, Future):
return fut
-
assert isinstance(fut, concurrent.futures.Future), \
'concurrent.futures.Future is expected, got {!r}'.format(fut)
-
if loop is None:
loop = events.get_event_loop()
+ new_future = Future(loop=loop)
- new_future = Future(loop=loop)
+ def _check_cancel_other(f):
+ if f.cancelled():
+ fut.cancel()
+
+ new_future.add_done_callback(_check_cancel_other)
fut.add_done_callback(
lambda future: loop.call_soon_threadsafe(
new_future._copy_state, fut))
diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py
--- a/Lib/test/test_asyncio/test_futures.py
+++ b/Lib/test/test_asyncio/test_futures.py
@@ -241,6 +241,24 @@
f2 = futures.wrap_future(f1)
self.assertIs(m_events.get_event_loop.return_value, f2._loop)
+ def test_wrap_future_cancel(self):
+ f1 = concurrent.futures.Future()
+ f2 = futures.wrap_future(f1, loop=self.loop)
+ f2.cancel()
+ test_utils.run_briefly(self.loop)
+ self.assertTrue(f1.cancelled())
+ self.assertTrue(f2.cancelled())
+
+ def test_wrap_future_cancel2(self):
+ f1 = concurrent.futures.Future()
+ f2 = futures.wrap_future(f1, loop=self.loop)
+ f1.set_result(42)
+ f2.cancel()
+ test_utils.run_briefly(self.loop)
+ self.assertFalse(f1.cancelled())
+ self.assertEqual(f1.result(), 42)
+ self.assertTrue(f2.cancelled())
+
class FutureDoneCallbackTests(unittest.TestCase):
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list