mirror of
https://github.com/python/cpython.git
synced 2025-08-19 00:00:48 +00:00
[3.9] bpo-45997: Fix asyncio.Semaphore re-acquiring order (GH-31910) (GH-32049)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>.
(cherry picked from commit 32e77154dd
)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
This commit is contained in:
parent
2d5e9f8d62
commit
f47984b560
3 changed files with 36 additions and 6 deletions
|
@ -961,6 +961,32 @@ class SemaphoreTests(unittest.IsolatedAsyncioTestCase):
|
|||
sem.release()
|
||||
self.assertFalse(sem.locked())
|
||||
|
||||
async def test_acquire_fifo_order(self):
|
||||
sem = asyncio.Semaphore(1)
|
||||
result = []
|
||||
|
||||
async def coro(tag):
|
||||
await sem.acquire()
|
||||
result.append(f'{tag}_1')
|
||||
await asyncio.sleep(0.01)
|
||||
sem.release()
|
||||
|
||||
await sem.acquire()
|
||||
result.append(f'{tag}_2')
|
||||
await asyncio.sleep(0.01)
|
||||
sem.release()
|
||||
|
||||
t1 = asyncio.create_task(coro('c1'))
|
||||
t2 = asyncio.create_task(coro('c2'))
|
||||
t3 = asyncio.create_task(coro('c3'))
|
||||
|
||||
await asyncio.gather(t1, t2, t3)
|
||||
|
||||
self.assertEqual(
|
||||
['c1_1', 'c2_1', 'c3_1', 'c1_2', 'c2_2', 'c3_2'],
|
||||
result
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue