Skip to content

Commit

Permalink
Merge pull request #90 from chrhansk/hotfix-runner-time-limit
Browse files Browse the repository at this point in the history
Fix ignored time limit in runners
  • Loading branch information
chrhansk authored May 22, 2024
2 parents f2c2107 + d014c2c commit 24e65ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
project = "pygradflow"
copyright = "2023, Christoph Hansknecht"
author = "Christoph Hansknecht"
release = "0.4.20"
release = "0.4.21"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
58 changes: 30 additions & 28 deletions pygradflow/runners/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import os
from abc import ABC, abstractmethod
from concurrent.futures import FIRST_COMPLETED, ProcessPoolExecutor, wait
from multiprocessing import TimeoutError, cpu_count
from multiprocessing.pool import ThreadPool
from multiprocessing import cpu_count

import numpy as np

Expand All @@ -19,46 +18,49 @@
formatter = logging.Formatter("%(asctime)s:%(name)s:%(levelname)s:%(message)s")


def try_solve_instance(instance, params, log_filename, verbose):
try:
logger.handlers.clear()
def solve_instance(instance, params, log_filename, verbose):
logger.handlers.clear()

np.seterr(divide="raise", over="raise", invalid="raise")
np.seterr(divide="raise", over="raise", invalid="raise")

handler = logging.FileHandler(log_filename)
handler = logging.FileHandler(log_filename)
handler.setFormatter(formatter)
logger.addHandler(handler)

if verbose:
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

if verbose:
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.setLevel(logging.INFO)
logging.captureWarnings(True)
warn_logger = logging.getLogger("py.warnings")
warn_logger.addHandler(handler)
warn_logger.setLevel(logging.WARN)

logging.captureWarnings(True)
warn_logger = logging.getLogger("py.warnings")
warn_logger.addHandler(handler)
warn_logger.setLevel(logging.WARN)
return instance.solve(params)

def solve():
return instance.solve(params)

def try_solve_instance(instance, params, log_filename, verbose):
try:
# No time limit
if params.time_limit == np.inf:
return (instance, solve())
return (instance, solve_instance(instance, params, log_filename, verbose))

# Solve in thread pool so we can
# await the result
thread_pool = ThreadPool(1)
with ProcessPoolExecutor(1) as pool:
future = pool.submit(
solve_instance, instance, params, log_filename, verbose
)
done, _ = wait([future], timeout=(params.time_limit + 10))

try:
res = thread_pool.apply_async(solve)
result = res.get(params.time_limit)
if len(done) == 0:
logger.error("Reached timeout, aborting")
return (instance, "timeout")

result = next(iter(done)).result()
return (instance, result)
except TimeoutError:
logger.error("Reached timeout, aborting")
return (instance, "timeout")

except Exception as exc:
logger.error("Error solving %s", instance.name)
logger.exception(exc, exc_info=(type(exc), exc, exc.__traceback__))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pygradflow"
version = "0.4.20"
version = "0.4.21"
description = "PyGradFlow is a simple implementation of the sequential homotopy method to be used to solve general nonlinear programs."
authors = ["Christoph Hansknecht <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 24e65ae

Please sign in to comment.