Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Minor improvements to prepare for freethreading (no GIL) capabilities #94

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

Merged
merged 7 commits into from
Feb 14, 2025
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
11 changes: 11 additions & 0 deletions constraint/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
from constraint.solvers import Solver, OptimizedBacktrackingSolver, ParallelSolver
from constraint.parser import compile_to_constraints

try:
from sys import _is_gil_enabled
freethreading = _is_gil_enabled()
except ImportError:
freethreading = False


class Problem:
"""Class used to define a problem and retrieve solutions."""
Expand All @@ -26,9 +32,14 @@ def __init__(self, solver: Solver=None):
self._str_constraints: list[str] = []
self._variables: dict[Hashable, Domain] = {}

# check if solver is instance instead of class
assert isinstance(self._solver, Solver), f"`solver` is not instance of Solver class (is {type(self._solver)})."

# warn for experimental parallel solver
if isinstance(self._solver, ParallelSolver):
warn("ParallelSolver is currently experimental, and unlikely to be faster than OptimizedBacktrackingSolver. Please report any issues.") # future: remove # noqa E501
if not self._solver._process_mode and not freethreading:
warn("Using the ParallelSolver in ThreadPool mode without freethreading will cause poor performance.")

def reset(self):
"""Reset the current problem definition.
Expand Down
Loading