VyLala commited on
Commit
5ae46d2
·
verified ·
1 Parent(s): b81b20e

Update pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline.py +56 -8
pipeline.py CHANGED
@@ -191,9 +191,44 @@ 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)
@@ -204,24 +239,37 @@ def run_with_timeout(func, args=(), kwargs={}, timeout=30):
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
 
 
225
  def time_it(func, *args, **kwargs):
226
  """
227
  Measure how long a function takes to run and return its result + time.
 
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
  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
 
271
 
272
+
273
  def time_it(func, *args, **kwargs):
274
  """
275
  Measure how long a function takes to run and return its result + time.