@@ -39,15 +39,12 @@ def submit(
3939 if self .executor is None :
4040 future = Future ()
4141 future .set_result (fn (* args , ** kwargs ))
42- return
42+ return None
4343
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
5148
5249 def wait (self , path : Optional [Path ] = None ) -> None :
5350 """Wait for tasks to complete.
@@ -61,17 +58,12 @@ def wait(self, path: Optional[Path] = None) -> None:
6158 """
6259 if self .executor is None :
6360 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 ()
7567
7668
7769def fake_job (i : int ) -> int :
@@ -82,18 +74,6 @@ def fake_job(i: int) -> int:
8274
8375if __name__ == "__main__" :
8476 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 )
9777 for i in range (10 ):
9878 executor .submit (Path (f"test{ i } .txt" ), fake_job , i )
9979 executor .wait ()
0 commit comments