Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
fix xdist
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Nov 7, 2024
1 parent 21d2517 commit 2b62a72
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ cmake.source-dir = "src/faebryk/core/cpp"
[tool.pytest]
[tool.pytest.ini_options]
# loadscope to run tests for each file in same worker
addopts = ["--import-mode=importlib", "--numprocesses=auto", "--dist=loadscope"]
addopts = ["--import-mode=importlib", "--numprocesses=auto"]
filterwarnings = ["ignore:.*:DeprecationWarning"]
testpaths = ["test"]

Expand Down
49 changes: 25 additions & 24 deletions src/faebryk/core/cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from importlib.metadata import Distribution

from faebryk.libs.util import ConfigFlag, at_exit
from faebryk.libs.util import ConfigFlag, at_exit, global_lock

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -63,29 +63,30 @@ def compile_and_load():
if arch in ["arm64", "x86_64"]:
other_flags += [f"-DCMAKE_OSX_ARCHITECTURES={arch}"]

run_live(
[
"cmake",
"-S",
str(_cmake_dir),
"-B",
str(_build_dir),
"-DEDITABLE=1",
"-DPython_EXECUTABLE=" + sys.executable,
*other_flags,
],
logger=logger,
)
run_live(
[
"cmake",
"--build",
str(_build_dir),
"--",
"-j",
],
logger=logger,
)
with global_lock(_build_dir / "lock", timeout_s=60):
run_live(
[
"cmake",
"-S",
str(_cmake_dir),
"-B",
str(_build_dir),
"-DEDITABLE=1",
"-DPython_EXECUTABLE=" + sys.executable,
*other_flags,
],
logger=logger,
)
run_live(
[
"cmake",
"--build",
str(_build_dir),
"--",
"-j",
],
logger=logger,
)

if not _build_dir.exists():
raise RuntimeError("build directory not found")
Expand Down
37 changes: 37 additions & 0 deletions src/faebryk/libs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import select
import subprocess
import sys
import time
from abc import abstractmethod
from collections import defaultdict
from contextlib import contextmanager
from dataclasses import dataclass, fields
from enum import StrEnum
from itertools import chain
from pathlib import Path
from textwrap import indent
from typing import (
Any,
Expand All @@ -33,6 +35,7 @@
get_origin,
)

import psutil
from tortoise import Model
from tortoise.queryset import QuerySet

Expand Down Expand Up @@ -1174,3 +1177,37 @@ def run_live(
)

return "\n".join(stdout), process


@contextmanager
def global_lock(lock_file_path: Path, timeout_s: float | None = None):
# TODO consider using filelock instead

lock_file_path.parent.mkdir(parents=True, exist_ok=True)

start_time = time.time()
while try_or(
lambda: bool(lock_file_path.touch(exist_ok=False)),
default=True,
catch=FileExistsError,
):
# check if pid still alive
try:
pid = int(lock_file_path.read_text())
except ValueError:
lock_file_path.unlink()
continue
assert pid != os.getpid()
if not psutil.pid_exists(pid):
lock_file_path.unlink()
continue
if timeout_s and time.time() - start_time > timeout_s:
raise TimeoutError()
time.sleep(0.1)

# write our pid to the lock file
lock_file_path.write_text(str(os.getpid()))
try:
yield
finally:
lock_file_path.unlink()

0 comments on commit 2b62a72

Please sign in to comment.