Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions deeplabcut/gui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from pathlib import Path
from typing import List
from urllib.error import URLError
from concurrent.futures import ThreadPoolExecutor, TimeoutError
import qdarkstyle
import multiprocessing

import deeplabcut
from deeplabcut import auxiliaryfunctions, VERSION, compat
Expand All @@ -42,9 +42,30 @@


def call_with_timeout(func, timeout, *args, **kwargs):
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(func, *args, **kwargs)
return future.result(timeout=timeout)
def wrapper(queue, *args, **kwargs):
try:
result = func(*args, **kwargs)
queue.put(result) # Pass the result back via the queue
except Exception as e:
queue.put(e) # Pass any exception back via the queue

queue = multiprocessing.Queue()
process = multiprocessing.Process(target=wrapper, args=(queue, *args), kwargs=kwargs)
process.start()
process.join(timeout)

if process.is_alive():
process.terminate() # Forcefully terminate the process
process.join()
raise TimeoutError(f"Function {func.__name__} did not complete within {timeout} seconds.")

if not queue.empty():
result = queue.get()
if isinstance(result, Exception):
raise result # Re-raise the exception if it occurred in the function
return result
else:
raise TimeoutError(f"Function {func.__name__} completed but did not return a result.")


def _check_for_updates(silent=True):
Expand Down
33 changes: 33 additions & 0 deletions tests/gui/test_gui_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/DeepLabCut
#
# Please see AUTHORS for contributors.
# https://github.com/DeepLabCut/DeepLabCut/blob/master/AUTHORS
#
# Licensed under GNU Lesser General Public License v3.0
#
import pytest
import time
from deeplabcut.gui.window import call_with_timeout

def test_call_with_timeout():
def succeeding_method(parameter):
return parameter

parameter = (10, "Hello test")
assert call_with_timeout(succeeding_method, 1, parameter) == parameter

def failing_method():
raise ValueError("Raise value error on purpose")

with pytest.raises(ValueError):
call_with_timeout(failing_method, timeout=1)

def hanging_method():
while True:
time.sleep(1)

with pytest.raises(TimeoutError):
call_with_timeout(hanging_method, timeout=1)