@@ -57,38 +57,46 @@ def _ordered_generator(
57
57
) -> Iterator [list [Out ]]:
58
58
"""Generate results in their original order."""
59
59
futures = deque ()
60
+ executor_shutdown = False
60
61
61
62
# Pre-submit initial batch of futures
62
63
for _ in range (self .max_workers + 1 ):
64
+ if executor_shutdown :
65
+ break
63
66
try :
64
67
chunk = next (chunks_iter )
65
68
futures .append (executor .submit (_worker_process_chunk , transformer , context_handle , chunk ))
66
69
except StopIteration :
67
70
break
68
71
except RuntimeError as e :
69
72
if "cannot schedule new futures after shutdown" in str (e ):
73
+ executor_shutdown = True
70
74
break
71
75
raise
72
76
73
77
while futures :
74
78
try :
75
79
yield futures .popleft ().result ()
76
80
77
- # Try to submit the next chunk
78
- try :
79
- chunk = next (chunks_iter )
80
- futures .append (executor .submit (_worker_process_chunk , transformer , context_handle , chunk ))
81
- except StopIteration :
82
- continue
83
- except RuntimeError as e :
84
- if "cannot schedule new futures after shutdown" in str (e ):
85
- # Executor is shut down, stop submitting new work
86
- break
87
- raise
81
+ # Try to submit the next chunk only if executor is not shutdown
82
+ if not executor_shutdown :
83
+ try :
84
+ chunk = next (chunks_iter )
85
+ futures .append (executor .submit (_worker_process_chunk , transformer , context_handle , chunk ))
86
+ except StopIteration :
87
+ continue
88
+ except RuntimeError as e :
89
+ if "cannot schedule new futures after shutdown" in str (e ):
90
+ executor_shutdown = True
91
+ continue
92
+ raise
88
93
except Exception :
89
94
# Cancel remaining futures and re-raise
90
95
for future in futures :
91
- future .cancel ()
96
+ try :
97
+ future .cancel ()
98
+ except Exception :
99
+ pass # Ignore cancellation errors
92
100
futures .clear ()
93
101
raise
94
102
@@ -101,37 +109,44 @@ def _unordered_generator(
101
109
) -> Iterator [list [Out ]]:
102
110
"""Generate results as they complete."""
103
111
futures = set ()
112
+ executor_shutdown = False
104
113
105
114
# Pre-submit initial batch
106
- try :
107
- for chunk in itertools .islice (chunks_iter , self .max_workers + 1 ):
115
+ for chunk in itertools .islice (chunks_iter , self .max_workers + 1 ):
116
+ if executor_shutdown :
117
+ break
118
+ try :
108
119
futures .add (executor .submit (_worker_process_chunk , transformer , context_handle , chunk ))
109
- except RuntimeError as e :
110
- if "cannot schedule new futures after shutdown" in str (e ):
111
- # If we can't submit any futures, there's nothing to process
112
- return
113
- raise
120
+ except RuntimeError as e :
121
+ if "cannot schedule new futures after shutdown" in str (e ):
122
+ executor_shutdown = True
123
+ break
124
+ raise
114
125
115
126
while futures :
116
127
try :
117
128
done , futures = wait (futures , return_when = FIRST_COMPLETED )
118
129
for future in done :
119
130
yield future .result ()
120
131
121
- # Try to submit next chunk if available
122
- try :
123
- chunk = next (chunks_iter )
124
- futures .add (executor .submit (_worker_process_chunk , transformer , context_handle , chunk ))
125
- except StopIteration :
126
- continue
127
- except RuntimeError as e :
128
- if "cannot schedule new futures after shutdown" in str (e ):
129
- # Executor is shut down, stop submitting new work
130
- break
131
- raise
132
+ # Try to submit next chunk only if executor is not shutdown
133
+ if not executor_shutdown :
134
+ try :
135
+ chunk = next (chunks_iter )
136
+ futures .add (executor .submit (_worker_process_chunk , transformer , context_handle , chunk ))
137
+ except StopIteration :
138
+ continue
139
+ except RuntimeError as e :
140
+ if "cannot schedule new futures after shutdown" in str (e ):
141
+ executor_shutdown = True
142
+ continue
143
+ raise
132
144
except Exception :
133
145
# Cancel remaining futures and re-raise
134
146
for future in futures :
135
- future .cancel ()
147
+ try :
148
+ future .cancel ()
149
+ except Exception :
150
+ pass # Ignore cancellation errors
136
151
futures .clear ()
137
152
raise
0 commit comments