Message285690
I may have mis-characterized the original report. Rephrasing just to make sure it is correctly addressed:
First, it is not about fairness. Sorry for the original characterization. I was trying to understand what was happening.
Second, the precise issue is whether a `yield from` can be relied upon for cooperative multitasking. Should other co-routines should be prevented from running or delayed if one routine is running and acquiring and releasing a lock using `yield from`.
It seemed at first sight that `yield from <anything>` should give the scheduler a chance to consider other routines. But the lock code above and including line #171 does not give control to the scheduler in a special case: https://github.com/python/cpython/blob/master/Lib/asyncio/locks.py#L171
Code Sample 1 (shows b() being delayed until a() is fully complete despite a() yielding many times)
=============
import asyncio
lock = asyncio.Lock()
def a ():
yield from lock.acquire()
for i in range(10):
print('b: ' + str(i))
if i % 2 == 0:
lock.release()
yield from lock.acquire()
lock.release()
async def b ():
print('hello')
asyncio.get_event_loop().run_until_complete(asyncio.gather(a(), b()))
print('done')
Code Sample 2
=============
(shows interleaving if an additional INITIAL yield from asyncio.sleep is inserted; removing the initial sleep removes the interleaving)
import asyncio
lock = asyncio.Lock()
def a ():
yield from lock.acquire()
yield from asyncio.sleep(0)
for i in range(10):
print('a: ' + str(i))
if i % 2 == 0:
lock.release()
yield from lock.acquire()
lock.release()
def b ():
yield from lock.acquire()
yield from asyncio.sleep(0)
for i in range(10):
print('b: ' + str(i))
if i % 2 == 0:
lock.release()
yield from lock.acquire()
lock.release()
asyncio.get_event_loop().run_until_complete(asyncio.gather(a(), b()))
print('done')
Thank you for your kind consideration. |
|
Date |
User |
Action |
Args |
2017-01-17 23:40:54 | anonymous2017 | set | recipients:
+ anonymous2017, gvanrossum, yselivanov |
2017-01-17 23:40:54 | anonymous2017 | set | messageid: <1484696454.51.0.148130613513.issue29303@psf.upfronthosting.co.za> |
2017-01-17 23:40:54 | anonymous2017 | link | issue29303 messages |
2017-01-17 23:40:53 | anonymous2017 | create | |
|