Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added windows support #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 35 additions & 1 deletion popper/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from contextlib import contextmanager
from .core import Literal
from math import comb
import threading

clingo.script.enable_python()

Expand Down Expand Up @@ -73,6 +74,9 @@ class TimeoutError(Exception):

def handler(signum, frame):
raise TimeoutError()

if not (hasattr(signal, 'SIGALRM') and hasattr(signal, 'alarm')):
return _windows_timeout(settings, func, args, kwargs, timeout_duration)

# set the timeout handler
signal.signal(signal.SIGALRM, handler)
Expand All @@ -92,6 +96,36 @@ def handler(signum, frame):

return result

def _windows_timeout(settings, func, args=(), kwargs={}, timeout_duration=1):
"""
A replacement for the `timeout` function that works on Windows, since the `signal` module is not fully supported on Windows.
"""
result = None

def target():
nonlocal result
try:
result = func(*args, **kwargs)
except Exception as e:
result = e

thread = threading.Thread(target=target)
thread.start()
thread.join(timeout_duration)

if thread.is_alive():
# If the thread is still alive after the timeout_duration, it means it timed out
thread.join() # Ensure the thread terminates properly
settings.logger.warn(f'TIMEOUT OF {int(settings.timeout)} SECONDS EXCEEDED')
# raise TimeoutError(f"Execution of {func.__name__} timed out after {timeout_duration} seconds")
return result

if isinstance(result, Exception):
# If the function raised an exception, re-raise it here
raise result

return result

def load_kbpath(kbpath):
def fix_path(filename):
full_filename = os.path.join(kbpath, filename)
Expand Down Expand Up @@ -587,7 +621,7 @@ def load_types(settings):
def bias_order(settings, max_size):

if not (settings.no_bias or settings.order_space):
return [(size_literals, settings.max_vars, settings.max_rules, None) for size_literals in range(1, max_size+1)]
return [(size_literals, settings.max_vars, settings.max_rules, None) for size_literals in range(1, max_size)]

# if settings.search_order is None:
ret = []
Expand Down