@@ -39,15 +39,12 @@ def submit(
39
39
if self .executor is None :
40
40
future = Future ()
41
41
future .set_result (fn (* args , ** kwargs ))
42
- return
42
+ return None
43
43
44
- if path not in self .working_map :
45
- future = self .executor .submit (fn , * args , ** kwargs )
46
- self .working_map [path ] = future
47
- else :
48
- future = self .working_map [path ]
49
- future .add_done_callback (lambda _ : self .working_map .pop (path , None ))
50
- future .add_done_callback (lambda _ : self .submit (path , fn , * args , ** kwargs ))
44
+ assert path not in self .working_map , "path already in working_map"
45
+ future = self .executor .submit (fn , * args , ** kwargs )
46
+ self .working_map [path ] = future
47
+ return future
51
48
52
49
def wait (self , path : Optional [Path ] = None ) -> None :
53
50
"""Wait for tasks to complete.
@@ -61,17 +58,12 @@ def wait(self, path: Optional[Path] = None) -> None:
61
58
"""
62
59
if self .executor is None :
63
60
return
64
- if path is not None and path in self .working_map :
65
- self .working_map .pop (path , None ).result ()
66
- # may have chained callback, so we need to wait again
67
- self .wait (path )
68
-
69
- while self .working_map :
70
- # Process one task for each for-loop
71
- # for map might be changed during the loop
72
- for path in self .working_map :
73
- self .wait (path )
74
- break
61
+ if path is None :
62
+ for future in self .working_map .values ():
63
+ future .result ()
64
+ self .working_map .clear ()
65
+ elif future := self .working_map .pop (path , None ):
66
+ future .result ()
75
67
76
68
77
69
def fake_job (i : int ) -> int :
@@ -82,18 +74,6 @@ def fake_job(i: int) -> int:
82
74
83
75
if __name__ == "__main__" :
84
76
executor = FileTaskExecutor (concurrent = 0 )
85
- for i in range (10 ):
86
- executor .submit (Path (f"test{ i } .txt" ), fake_job , i )
87
- for i in range (10 ):
88
- executor .submit (Path (f"test{ i } .txt" ), fake_job , i )
89
- for i in range (10 ):
90
- executor .submit (Path (f"test{ i } .txt" ), fake_job , i )
91
- for i in range (10 ):
92
- executor .submit (Path (f"test{ i } .txt" ), fake_job , i )
93
- for i in range (10 ):
94
- executor .submit (Path (f"test{ i } .txt" ), fake_job , i )
95
- for i in range (10 ):
96
- executor .submit (Path (f"test{ i } .txt" ), fake_job , i )
97
77
for i in range (10 ):
98
78
executor .submit (Path (f"test{ i } .txt" ), fake_job , i )
99
79
executor .wait ()
0 commit comments