mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-32193: Convert asyncio to async/await usage (#4753)
* Convert asyncio/tasks.py to async/await * Convert asyncio/queues.py to async/await * Convert asyncio/test_utils.py to async/await * Convert asyncio/base_subprocess.py to async/await * Convert asyncio/subprocess.py to async/await * Convert asyncio/streams.py to async/await * Fix comments * Convert asyncio/locks.py to async/await * Convert asyncio.sleep to async def * Add a comment * Add missing news * Convert stubs from AbstrctEventLoop to async functions * Convert subprocess_shell/subprocess_exec * Convert connect_read_pipe/connect_write_pip to async/await syntax * Convert create_datagram_endpoint * Convert create_unix_server/create_unix_connection * Get rid of old style coroutines in unix_events.py * Convert selector_events.py to async/await * Convert wait_closed and create_connection * Drop redundant line * Convert base_events.py * Code cleanup * Drop redundant comments * Fix indentation * Add explicit tests for compatibility between old and new coroutines * Convert windows event loop to use async/await * Fix double awaiting of async function * Convert asyncio/locks.py * Improve docstring * Convert tests to async/await * Convert more tests * Convert more tests * Convert more tests * Convert tests * Improve test
This commit is contained in:
parent
ede157331b
commit
5f841b5538
22 changed files with 647 additions and 771 deletions
|
@ -69,21 +69,18 @@ class LockTests(test_utils.TestCase):
|
|||
|
||||
self.assertTrue(self.loop.run_until_complete(lock.acquire()))
|
||||
|
||||
@asyncio.coroutine
|
||||
def c1(result):
|
||||
if (yield from lock.acquire()):
|
||||
async def c1(result):
|
||||
if await lock.acquire():
|
||||
result.append(1)
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c2(result):
|
||||
if (yield from lock.acquire()):
|
||||
async def c2(result):
|
||||
if await lock.acquire():
|
||||
result.append(2)
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c3(result):
|
||||
if (yield from lock.acquire()):
|
||||
async def c3(result):
|
||||
if await lock.acquire():
|
||||
result.append(3)
|
||||
return True
|
||||
|
||||
|
@ -145,12 +142,11 @@ class LockTests(test_utils.TestCase):
|
|||
# Setup: A has the lock, b and c are waiting.
|
||||
lock = asyncio.Lock(loop=self.loop)
|
||||
|
||||
@asyncio.coroutine
|
||||
def lockit(name, blocker):
|
||||
yield from lock.acquire()
|
||||
async def lockit(name, blocker):
|
||||
await lock.acquire()
|
||||
try:
|
||||
if blocker is not None:
|
||||
yield from blocker
|
||||
await blocker
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
|
@ -294,19 +290,16 @@ class EventTests(test_utils.TestCase):
|
|||
|
||||
result = []
|
||||
|
||||
@asyncio.coroutine
|
||||
def c1(result):
|
||||
if (yield from ev.wait()):
|
||||
async def c1(result):
|
||||
if await ev.wait():
|
||||
result.append(1)
|
||||
|
||||
@asyncio.coroutine
|
||||
def c2(result):
|
||||
if (yield from ev.wait()):
|
||||
async def c2(result):
|
||||
if await ev.wait():
|
||||
result.append(2)
|
||||
|
||||
@asyncio.coroutine
|
||||
def c3(result):
|
||||
if (yield from ev.wait()):
|
||||
async def c3(result):
|
||||
if await ev.wait():
|
||||
result.append(3)
|
||||
|
||||
t1 = asyncio.Task(c1(result), loop=self.loop)
|
||||
|
@ -359,9 +352,8 @@ class EventTests(test_utils.TestCase):
|
|||
ev = asyncio.Event(loop=self.loop)
|
||||
result = []
|
||||
|
||||
@asyncio.coroutine
|
||||
def c1(result):
|
||||
if (yield from ev.wait()):
|
||||
async def c1(result):
|
||||
if await ev.wait():
|
||||
result.append(1)
|
||||
return True
|
||||
|
||||
|
@ -408,24 +400,21 @@ class ConditionTests(test_utils.TestCase):
|
|||
cond = asyncio.Condition(loop=self.loop)
|
||||
result = []
|
||||
|
||||
@asyncio.coroutine
|
||||
def c1(result):
|
||||
yield from cond.acquire()
|
||||
if (yield from cond.wait()):
|
||||
async def c1(result):
|
||||
await cond.acquire()
|
||||
if await cond.wait():
|
||||
result.append(1)
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c2(result):
|
||||
yield from cond.acquire()
|
||||
if (yield from cond.wait()):
|
||||
async def c2(result):
|
||||
await cond.acquire()
|
||||
if await cond.wait():
|
||||
result.append(2)
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c3(result):
|
||||
yield from cond.acquire()
|
||||
if (yield from cond.wait()):
|
||||
async def c3(result):
|
||||
await cond.acquire()
|
||||
if await cond.wait():
|
||||
result.append(3)
|
||||
return True
|
||||
|
||||
|
@ -522,10 +511,9 @@ class ConditionTests(test_utils.TestCase):
|
|||
|
||||
result = []
|
||||
|
||||
@asyncio.coroutine
|
||||
def c1(result):
|
||||
yield from cond.acquire()
|
||||
if (yield from cond.wait_for(predicate)):
|
||||
async def c1(result):
|
||||
await cond.acquire()
|
||||
if await cond.wait_for(predicate):
|
||||
result.append(1)
|
||||
cond.release()
|
||||
return True
|
||||
|
@ -567,26 +555,23 @@ class ConditionTests(test_utils.TestCase):
|
|||
cond = asyncio.Condition(loop=self.loop)
|
||||
result = []
|
||||
|
||||
@asyncio.coroutine
|
||||
def c1(result):
|
||||
yield from cond.acquire()
|
||||
if (yield from cond.wait()):
|
||||
async def c1(result):
|
||||
await cond.acquire()
|
||||
if await cond.wait():
|
||||
result.append(1)
|
||||
cond.release()
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c2(result):
|
||||
yield from cond.acquire()
|
||||
if (yield from cond.wait()):
|
||||
async def c2(result):
|
||||
await cond.acquire()
|
||||
if await cond.wait():
|
||||
result.append(2)
|
||||
cond.release()
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c3(result):
|
||||
yield from cond.acquire()
|
||||
if (yield from cond.wait()):
|
||||
async def c3(result):
|
||||
await cond.acquire()
|
||||
if await cond.wait():
|
||||
result.append(3)
|
||||
cond.release()
|
||||
return True
|
||||
|
@ -623,18 +608,16 @@ class ConditionTests(test_utils.TestCase):
|
|||
|
||||
result = []
|
||||
|
||||
@asyncio.coroutine
|
||||
def c1(result):
|
||||
yield from cond.acquire()
|
||||
if (yield from cond.wait()):
|
||||
async def c1(result):
|
||||
await cond.acquire()
|
||||
if await cond.wait():
|
||||
result.append(1)
|
||||
cond.release()
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c2(result):
|
||||
yield from cond.acquire()
|
||||
if (yield from cond.wait()):
|
||||
async def c2(result):
|
||||
await cond.acquire()
|
||||
if await cond.wait():
|
||||
result.append(2)
|
||||
cond.release()
|
||||
return True
|
||||
|
@ -791,27 +774,23 @@ class SemaphoreTests(test_utils.TestCase):
|
|||
self.assertTrue(self.loop.run_until_complete(sem.acquire()))
|
||||
self.assertFalse(sem.locked())
|
||||
|
||||
@asyncio.coroutine
|
||||
def c1(result):
|
||||
yield from sem.acquire()
|
||||
async def c1(result):
|
||||
await sem.acquire()
|
||||
result.append(1)
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c2(result):
|
||||
yield from sem.acquire()
|
||||
async def c2(result):
|
||||
await sem.acquire()
|
||||
result.append(2)
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c3(result):
|
||||
yield from sem.acquire()
|
||||
async def c3(result):
|
||||
await sem.acquire()
|
||||
result.append(3)
|
||||
return True
|
||||
|
||||
@asyncio.coroutine
|
||||
def c4(result):
|
||||
yield from sem.acquire()
|
||||
async def c4(result):
|
||||
await sem.acquire()
|
||||
result.append(4)
|
||||
return True
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue