@@ -2437,8 +2437,11 @@ def test_namespace(self):
2437
2437
#
2438
2438
#
2439
2439
2440
- def sqr (x , wait = 0.0 ):
2441
- time .sleep (wait )
2440
+ def sqr (x , wait = 0.0 , event = None ):
2441
+ if event is None :
2442
+ time .sleep (wait )
2443
+ else :
2444
+ event .wait (wait )
2442
2445
return x * x
2443
2446
2444
2447
def mul (x , y ):
@@ -2577,10 +2580,18 @@ def test_async(self):
2577
2580
self .assertTimingAlmostEqual (get .elapsed , TIMEOUT1 )
2578
2581
2579
2582
def test_async_timeout (self ):
2580
- res = self .pool .apply_async (sqr , (6 , TIMEOUT2 + support .SHORT_TIMEOUT ))
2581
- get = TimingWrapper (res .get )
2582
- self .assertRaises (multiprocessing .TimeoutError , get , timeout = TIMEOUT2 )
2583
- self .assertTimingAlmostEqual (get .elapsed , TIMEOUT2 )
2583
+ p = self .Pool (3 )
2584
+ try :
2585
+ event = threading .Event () if self .TYPE == 'threads' else None
2586
+ res = p .apply_async (sqr , (6 , TIMEOUT2 + support .SHORT_TIMEOUT , event ))
2587
+ get = TimingWrapper (res .get )
2588
+ self .assertRaises (multiprocessing .TimeoutError , get , timeout = TIMEOUT2 )
2589
+ self .assertTimingAlmostEqual (get .elapsed , TIMEOUT2 )
2590
+ finally :
2591
+ if event is not None :
2592
+ event .set ()
2593
+ p .terminate ()
2594
+ p .join ()
2584
2595
2585
2596
def test_imap (self ):
2586
2597
it = self .pool .imap (sqr , list (range (10 )))
@@ -2682,10 +2693,11 @@ def test_make_pool(self):
2682
2693
2683
2694
def test_terminate (self ):
2684
2695
# Simulate slow tasks which take "forever" to complete
2696
+ p = self .Pool (3 )
2685
2697
args = [support .LONG_TIMEOUT for i in range (10_000 )]
2686
- result = self . pool .map_async (time .sleep , args , chunksize = 1 )
2687
- self . pool .terminate ()
2688
- self . pool .join ()
2698
+ result = p .map_async (time .sleep , args , chunksize = 1 )
2699
+ p .terminate ()
2700
+ p .join ()
2689
2701
2690
2702
def test_empty_iterable (self ):
2691
2703
# See Issue 12157
0 commit comments