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