@@ -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,68 @@ 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_producer (q ):
649
+ print ('producer' ,'loading jobs' )
650
+ for x in range (10 ):
651
+ await q .put (x )
652
+ print ('producer' ,'await q.join' )
653
+ await q .join ()
654
+ print ('producer' ,'joined!' , 'task done!' )
655
+ async def q_task_done_join_go ():
656
+ q = Queue ()
657
+
658
+ consumer_task = asyncio .create_task (q_task_done_join_consumer (q ))
659
+ producer_task = asyncio .create_task (q_task_done_join_producer (q ))
660
+ await asyncio .sleep (0 )
661
+
662
+ print ('test' ,'await q.join' )
663
+ await q .join ()
664
+ print ('test' ,'all jobs done!' )
665
+
666
+ print ('test' ,'join again' )
667
+ await q .join ()
668
+
669
+ await asyncio .sleep (0 )
670
+ print ('test' ,'producer_task.done()?' , producer_task .done ())
671
+
672
+ consumer_task .cancel ()
673
+ await asyncio .gather (consumer_task , return_exceptions = True )
674
+
675
+ print ('test' ,'DONE' )
676
+
677
+
678
+ def q_task_done_join_test ():
679
+ printexp ('''Test Queue task_done/join behaviors
680
+ producer loading jobs
681
+ producer await q.join
682
+ test await q.join
683
+ consumer got/processing 0
684
+ consumer got/processing 1
685
+ consumer got/processing 2
686
+ consumer got/processing 3
687
+ consumer got/processing 4
688
+ consumer got/processing 5
689
+ consumer got/processing 6
690
+ consumer got/processing 7
691
+ consumer got/processing 8
692
+ consumer got/processing 9
693
+ producer joined! task done!
694
+ test all jobs done!
695
+ test join again
696
+ test producer_task.done()? True
697
+ test DONE
698
+ ''' , 5 )
699
+ asyncio .run (q_task_done_join_go ())
700
+
701
+
639
702
# ************ ************
640
703
def test (n ):
641
704
try :
@@ -657,6 +720,8 @@ def test(n):
657
720
queue_test () # Test the Queue class.
658
721
elif n == 9 :
659
722
rbq_test () # Test the RingbufQueue class.
723
+ elif n == 10 :
724
+ q_task_done_join_test () # Test the Queue task_done/join behavior.
660
725
except KeyboardInterrupt :
661
726
print ('Interrupted' )
662
727
finally :
0 commit comments