@@ -576,8 +576,16 @@ def __repr__(self) -> str:
576
576
577
577
@asynccontextmanager
578
578
async def __inner_lock (self ) -> AsyncGenerator [bool , None ]:
579
- with self ._lock as r :
580
- yield r
579
+ b = self ._lock .acquire (blocking = False )
580
+ while not b :
581
+ await asyncio .sleep (0.01 )
582
+ b = self ._lock .acquire (blocking = False )
583
+
584
+ try :
585
+ yield b
586
+ finally :
587
+ if b :
588
+ self ._lock .release ()
581
589
582
590
async def acquire (self ) -> bool :
583
591
async with self .__inner_lock ():
@@ -595,13 +603,18 @@ async def acquire(self) -> bool:
595
603
596
604
try :
597
605
try :
598
- await fut
606
+ await asyncio .wait_for (fut , 15 ) # TODO remove this hack
607
+ except asyncio .TimeoutError :
608
+ pass
599
609
finally :
600
610
self ._waiters .remove (fut )
601
611
except asyncio .CancelledError :
612
+ wakeup = False
602
613
async with self .__inner_lock ():
603
- if not self ._locked :
604
- self ._wake_up_first ()
614
+ wakeup = not self ._locked
615
+
616
+ if wakeup :
617
+ self ._wake_up_first ()
605
618
raise
606
619
607
620
async with self .__inner_lock ():
@@ -610,12 +623,13 @@ async def acquire(self) -> bool:
610
623
return True
611
624
612
625
async def release (self ) -> None :
613
- if self ._locked :
614
- async with self .__inner_lock () :
626
+ async with self .__inner_lock () :
627
+ if self ._locked :
615
628
self ._locked = False
616
- self ._wake_up_first ()
617
- else :
618
- raise RuntimeError ("Lock is not acquired." )
629
+ else :
630
+ raise RuntimeError ("Lock is not acquired." )
631
+
632
+ self ._wake_up_first ()
619
633
620
634
def _wake_up_first (self ) -> None :
621
635
if not self ._waiters :
@@ -625,11 +639,14 @@ def _wake_up_first(self) -> None:
625
639
except StopIteration :
626
640
return
627
641
642
+ def s () -> None :
643
+ fut .set_result (True )
644
+
628
645
if not fut .done ():
629
646
if fut ._loop == asyncio .get_running_loop ():
630
647
fut .set_result (True )
631
648
else :
632
- fut ._loop .call_soon_threadsafe (fut . set_result , True )
649
+ fut ._loop .call_soon_threadsafe (s )
633
650
634
651
635
652
class FutureInfo :
0 commit comments