Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/c7n/executor.py: 40%

42 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:51 +0000

1# Copyright The Cloud Custodian Authors. 

2# SPDX-License-Identifier: Apache-2.0 

3from concurrent.futures import (ProcessPoolExecutor, ThreadPoolExecutor) # noqa 

4 

5import threading 

6 

7 

8class MainThreadExecutor: 

9 """ For running tests. 

10 

11 c7n_async == True -> catch exceptions and store them in the future. 

12 c7n_async == False -> let exceptions bubble up. 

13 """ 

14 

15 c7n_async = True 

16 

17 # For Dev/Unit Testing with concurrent.futures 

18 def __init__(self, *args, **kw): 

19 self.args = args 

20 self.kw = kw 

21 

22 def map(self, func, iterable): 

23 for args in iterable: 

24 yield func(args) 

25 

26 def submit(self, func, *args, **kw): 

27 try: 

28 return MainThreadFuture(func(*args, **kw)) 

29 except Exception as e: 

30 if self.c7n_async: 

31 return MainThreadFuture(None, exception=e) 

32 raise 

33 

34 def __enter__(self): 

35 return self 

36 

37 def __exit__(self, *args): 

38 return False 

39 

40 

41class MainThreadFuture: 

42 # For Dev/Unit Testing with concurrent.futures 

43 

44 def __init__(self, value, exception=None): 

45 self.value = value 

46 self._exception = exception 

47 # Sigh concurrent.futures pokes at privates 

48 self._state = 'FINISHED' 

49 self._waiters = [] 

50 self._condition = threading.Condition() 

51 

52 def cancel(self): 

53 return False 

54 

55 def cancelled(self): 

56 return False 

57 

58 def exception(self): 

59 return self._exception 

60 

61 def done(self): 

62 return True 

63 

64 def result(self, timeout=None): 

65 if self._exception: 

66 raise self._exception 

67 return self.value 

68 

69 def add_done_callback(self, fn): 

70 return fn(self)