Skip to content

Commit 36afefd

Browse files
committed
Refactor subprocess handling and add running_in_snakemake utility function
1 parent 24efd1e commit 36afefd

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

anypytools/abcutils.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,20 @@
2121
from contextlib import suppress
2222
from pathlib import Path
2323
from queue import Queue
24-
from subprocess import TimeoutExpired
24+
import subprocess
2525
from tempfile import NamedTemporaryFile
2626
from threading import RLock, Thread
2727
from typing import Generator, List
2828

2929
import numpy as np
30-
30+
from rich import print
3131
from rich.progress import (
32-
Progress,
3332
BarColumn,
33+
Progress,
3434
TextColumn,
3535
TimeElapsedColumn,
3636
TimeRemainingColumn,
3737
)
38-
from rich import print
3938

4039
from .macroutils import AnyMacro, MacroCommand
4140
from .tools import (
@@ -49,6 +48,7 @@
4948
getsubdirs,
5049
make_hash,
5150
parse_anybodycon_output,
51+
running_in_snakemake,
5252
silentremove,
5353
winepath,
5454
)
@@ -59,18 +59,19 @@
5959
"Task",
6060
]
6161

62-
if ON_WINDOWS:
63-
if "ANYPYTOOLS_DEBUG_USE_PYTHON_POPEN" in os.environ:
64-
logger.warning("Warning: Using Python's subprocess.Popen instead of JobPopen.")
65-
from subprocess import Popen
66-
else:
67-
from .jobpopen import JobPopen as Popen
62+
logger = logging.getLogger("abt.anypytools")
6863

69-
from subprocess import CREATE_NEW_PROCESS_GROUP
64+
if (
65+
ON_WINDOWS
66+
and not running_in_snakemake()
67+
and "ANYPYTOOLS_DEBUG_USE_PYTHON_POPEN" not in os.environ
68+
):
69+
from .jobpopen import JobPopen as Popen
7070
else:
71+
print("running with normal subprocess.Popen")
72+
logger.warning("running with normal subprocess.Popen")
7173
from subprocess import Popen
7274

73-
logger = logging.getLogger("abt.anypytools")
7475

7576
_thread_lock = RLock()
7677
_KILLED_BY_ANYPYTOOLS = 10
@@ -219,7 +220,7 @@ def execute_anybodycon(
219220
ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX)
220221
subprocess_flags = 0x8000000 # win32con.CREATE_NO_WINDOW?
221222
subprocess_flags |= priority
222-
subprocess_flags |= CREATE_NEW_PROCESS_GROUP
223+
subprocess_flags |= subprocess.CREATE_NEW_PROCESS_GROUP
223224
extra_kwargs = {"creationflags": subprocess_flags}
224225

225226
cmd = [
@@ -284,7 +285,7 @@ def execute_anybodycon(
284285
try:
285286
proc.wait(timeout=timeout)
286287
retcode = ctypes.c_int32(proc.returncode).value
287-
except TimeoutExpired:
288+
except subprocess.TimeoutExpired:
288289
proc.kill()
289290
proc.communicate()
290291
retcode = _TIMEDOUT_BY_ANYPYTOOLS
@@ -296,7 +297,7 @@ def execute_anybodycon(
296297
finally:
297298
if retcode is None:
298299
proc.kill()
299-
if ON_WINDOWS:
300+
if ON_WINDOWS and hasattr(proc, "_close_job_object"):
300301
proc._close_job_object(proc._win32_job)
301302
else:
302303
subprocess_container.remove(proc.pid)

anypytools/tools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from dataclasses import dataclass
3030
from pathlib import Path
3131
from typing import Any, Iterable
32+
import inspect
3233

3334
# external imports
3435
import numpy as np
@@ -68,6 +69,15 @@ def run_from_ipython():
6869
ON_WINDOWS = platform.system() == "Windows"
6970

7071

72+
def running_in_snakemake():
73+
"""Check if we are running inside snakemake make main process"""
74+
for frame in inspect.stack():
75+
if "snakemake\\site_packages" in frame.filename:
76+
return True
77+
else:
78+
return False
79+
80+
7181
def case_preserving_replace(string, old, new):
7282
"""Replace string while preserving the case"""
7383

0 commit comments

Comments
 (0)