VyLala commited on
Commit
fa217ce
·
verified ·
1 Parent(s): 82d496f

Update pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline.py +8 -55
pipeline.py CHANGED
@@ -191,44 +191,9 @@ def download_drive_file_content(file_id):
191
  # print(f"⏱️ Timeout exceeded ({timeout} sec) — function killed.")
192
  # return False, None
193
 
194
- # import multiprocessing
195
-
196
- # def run_with_timeout(func, args=(), kwargs={}, timeout=30):
197
- # def wrapper(q, *args, **kwargs):
198
- # try:
199
- # result = func(*args, **kwargs)
200
- # q.put((True, result))
201
- # except Exception as e:
202
- # q.put((False, e))
203
-
204
- # q = multiprocessing.Queue()
205
- # p = multiprocessing.Process(target=wrapper, args=(q, *args), kwargs=kwargs)
206
- # p.start()
207
- # p.join(timeout)
208
-
209
- # if p.is_alive():
210
- # p.terminate()
211
- # p.join()
212
- # print(f"⏱️ Timeout exceeded ({timeout} sec) — function killed.")
213
- # return False, None
214
-
215
- # if not q.empty():
216
- # success, result = q.get()
217
- # if success:
218
- # return True, result
219
- # else:
220
- # raise result # re-raise exception if needed
221
-
222
- # return False, None
223
-
224
  import multiprocessing
225
- import time
226
 
227
- def run_with_timeout(func, args=(), kwargs={}, timeout=30, stop_value=None):
228
- """
229
- Runs func in a separate process with optional timeout.
230
- If stop_value is provided and becomes True during execution, the process is killed early.
231
- """
232
  def wrapper(q, *args, **kwargs):
233
  try:
234
  result = func(*args, **kwargs)
@@ -239,32 +204,20 @@ def run_with_timeout(func, args=(), kwargs={}, timeout=30, stop_value=None):
239
  q = multiprocessing.Queue()
240
  p = multiprocessing.Process(target=wrapper, args=(q, *args), kwargs=kwargs)
241
  p.start()
 
242
 
243
- start_time = time.time()
244
- while p.is_alive():
245
- # Timeout check
246
- if timeout is not None and (time.time() - start_time) > timeout:
247
- p.terminate()
248
- p.join()
249
- print(f"⏱️ Timeout exceeded ({timeout} sec) — function killed.")
250
- return False, None
251
-
252
- # Stop flag check
253
- if stop_value is not None and stop_value.value:
254
- p.terminate()
255
- p.join()
256
- print("🛑 Stop flag detected — function killed early.")
257
- return False, None
258
-
259
- time.sleep(0.1) # avoid busy waiting
260
 
261
- # Process finished naturally
262
  if not q.empty():
263
  success, result = q.get()
264
  if success:
265
  return True, result
266
  else:
267
- raise result
268
 
269
  return False, None
270
 
 
191
  # print(f"⏱️ Timeout exceeded ({timeout} sec) — function killed.")
192
  # return False, None
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  import multiprocessing
 
195
 
196
+ def run_with_timeout(func, args=(), kwargs={}, timeout=30):
 
 
 
 
197
  def wrapper(q, *args, **kwargs):
198
  try:
199
  result = func(*args, **kwargs)
 
204
  q = multiprocessing.Queue()
205
  p = multiprocessing.Process(target=wrapper, args=(q, *args), kwargs=kwargs)
206
  p.start()
207
+ p.join(timeout)
208
 
209
+ if p.is_alive():
210
+ p.terminate()
211
+ p.join()
212
+ print(f"⏱️ Timeout exceeded ({timeout} sec) function killed.")
213
+ return False, None
 
 
 
 
 
 
 
 
 
 
 
 
214
 
 
215
  if not q.empty():
216
  success, result = q.get()
217
  if success:
218
  return True, result
219
  else:
220
+ raise result # re-raise exception if needed
221
 
222
  return False, None
223