@@ -34,6 +34,7 @@ def print_tests():
34
34
test(7) Test the Condition class.
35
35
test(8) Test the Queue class.
36
36
test(9) Test the RingbufQueue class.
37
+ test(10) Test the Queue task_done/join behavior.
37
38
'''
38
39
print ('\x1b [32m' )
39
40
print (st )
@@ -636,6 +637,70 @@ def rbq_test():
636
637
''' , 6 )
637
638
asyncio .run (rbq_go ())
638
639
640
+
641
+ # ************ Queue task_done/join test ************
642
+ async def q_task_done_join_consumer (q ):
643
+ while True :
644
+ r = await q .get ()
645
+ print ('consumer' , 'got/processing {}' .format (r ))
646
+ await asyncio .sleep (.5 )
647
+ q .task_done ()
648
+ async def q_task_done_join_waiter (q ):
649
+ print ('waiter' ,'await q.join' )
650
+ await q .join ()
651
+ print ('waiter' ,'joined!' , 'task done!' )
652
+ async def q_task_done_join_go ():
653
+ q = Queue ()
654
+
655
+ #empty queue should not block join
656
+ print ('test' , 'await empty q.join' )
657
+ await q .join ()
658
+ print ('test' , 'pass' )
659
+
660
+ consumer_task = asyncio .create_task (q_task_done_join_consumer (q ))
661
+ waiter_task = asyncio .create_task (q_task_done_join_waiter (q ))
662
+
663
+ #add jobs
664
+ for x in range (10 ):
665
+ await q .put (x )
666
+
667
+ print ('test' ,'await q.join' )
668
+ await q .join ()
669
+ print ('test' ,'all jobs done!' )
670
+
671
+ await asyncio .sleep (0 )
672
+ print ('test' ,'waiter_task.done()?' , waiter_task .done ())
673
+
674
+ consumer_task .cancel ()
675
+ await asyncio .gather (consumer_task , return_exceptions = True )
676
+
677
+ print ('test' ,'DONE' )
678
+
679
+
680
+ def q_task_done_join_test ():
681
+ printexp ('''Test Queue task_done/join behaviors
682
+ test await empty q.join
683
+ test pass
684
+ test await q.join
685
+ consumer got/processing 0
686
+ waiter await q.join
687
+ consumer got/processing 1
688
+ consumer got/processing 2
689
+ consumer got/processing 3
690
+ consumer got/processing 4
691
+ consumer got/processing 5
692
+ consumer got/processing 6
693
+ consumer got/processing 7
694
+ consumer got/processing 8
695
+ consumer got/processing 9
696
+ test all jobs done!
697
+ waiter joined! task done!
698
+ test waiter_task.done()? True
699
+ test DONE
700
+ ''' , 5 )
701
+ asyncio .run (q_task_done_join_go ())
702
+
703
+
639
704
# ************ ************
640
705
def test (n ):
641
706
try :
@@ -657,6 +722,8 @@ def test(n):
657
722
queue_test () # Test the Queue class.
658
723
elif n == 9 :
659
724
rbq_test () # Test the RingbufQueue class.
725
+ elif n == 10 :
726
+ q_task_done_join_test () # Test the Queue task_done/join behavior.
660
727
except KeyboardInterrupt :
661
728
print ('Interrupted' )
662
729
finally :
0 commit comments