Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df38a80

Browse files
committed
merge
2 parents 224b261 + 7a46564 commit df38a80

4 files changed

Lines changed: 47 additions & 3 deletions

File tree

Doc/library/asyncio.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
:mod:`asyncio` -- Asynchronous I/O, event loop, coroutines and tasks
2+
====================================================================
3+
4+
.. module:: asyncio
5+
:synopsis: Asynchronous I/O, event loop, coroutines and tasks.
6+
7+
.. versionadded:: 3.4
8+
9+
10+
Introduction
11+
------------
12+
13+
This package includes a pluggable event loop, transport and protocol
14+
abstractions similar to those in Twisted, and a higher-level scheduler
15+
for coroutines and tasks based on ``yield from`` (:PEP:`380`).
16+
17+
Full documentation is not yet ready; we hope to have it written
18+
before Python 3.4 leaves beta. Until then, the best reference is
19+
:PEP:`3156`. For a motivational primer on transports and protocols,
20+
see :PEP:`3153`.

Doc/library/concurrency.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ multitasking). Here's an overview:
2222
queue.rst
2323
select.rst
2424
selectors.rst
25+
asyncio.rst
2526

2627

2728
The following are support modules for some of the above services:

Lib/asyncio/futures.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ def _copy_state(self, other):
301301
The other Future may be a concurrent.futures.Future.
302302
"""
303303
assert other.done()
304+
if self.cancelled():
305+
return
304306
assert not self.done()
305307
if other.cancelled():
306308
self.cancel()
@@ -324,14 +326,17 @@ def wrap_future(fut, *, loop=None):
324326
"""Wrap concurrent.futures.Future object."""
325327
if isinstance(fut, Future):
326328
return fut
327-
328329
assert isinstance(fut, concurrent.futures.Future), \
329330
'concurrent.futures.Future is expected, got {!r}'.format(fut)
330-
331331
if loop is None:
332332
loop = events.get_event_loop()
333-
334333
new_future = Future(loop=loop)
334+
335+
def _check_cancel_other(f):
336+
if f.cancelled():
337+
fut.cancel()
338+
339+
new_future.add_done_callback(_check_cancel_other)
335340
fut.add_done_callback(
336341
lambda future: loop.call_soon_threadsafe(
337342
new_future._copy_state, fut))

Lib/test/test_asyncio/test_futures.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,24 @@ def run(arg):
241241
f2 = futures.wrap_future(f1)
242242
self.assertIs(m_events.get_event_loop.return_value, f2._loop)
243243

244+
def test_wrap_future_cancel(self):
245+
f1 = concurrent.futures.Future()
246+
f2 = futures.wrap_future(f1, loop=self.loop)
247+
f2.cancel()
248+
test_utils.run_briefly(self.loop)
249+
self.assertTrue(f1.cancelled())
250+
self.assertTrue(f2.cancelled())
251+
252+
def test_wrap_future_cancel2(self):
253+
f1 = concurrent.futures.Future()
254+
f2 = futures.wrap_future(f1, loop=self.loop)
255+
f1.set_result(42)
256+
f2.cancel()
257+
test_utils.run_briefly(self.loop)
258+
self.assertFalse(f1.cancelled())
259+
self.assertEqual(f1.result(), 42)
260+
self.assertTrue(f2.cancelled())
261+
244262

245263
class FutureDoneCallbackTests(unittest.TestCase):
246264

0 commit comments

Comments
 (0)