Skip to content

Commit

Permalink
use timeout in communicate
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Jan 29, 2025
1 parent c82793f commit 6947ffc
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/pymatgen/command_line/enumlib_caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import subprocess
from glob import glob
from shutil import which
from threading import Timer
from typing import TYPE_CHECKING

import numpy as np
Expand Down Expand Up @@ -120,6 +119,7 @@ def __init__(
self.structure = finder.get_refined_structure()
else:
self.structure = structure

self.min_cell_size = min_cell_size
self.max_cell_size = max_cell_size
self.symm_prec = symm_prec
Expand Down Expand Up @@ -290,23 +290,12 @@ def _run_multienum(self) -> int:
int: number of structures.
"""
with subprocess.Popen([ENUM_CMD], stdout=subprocess.PIPE, stdin=subprocess.PIPE, close_fds=True) as process:
if self.timeout:
timed_out = False
timer = Timer(self.timeout * 60, lambda p: p.kill(), [process])

try:
timer.start()
output = process.communicate()[0].decode("utf-8")
finally:
if not timer.is_alive():
timed_out = True
timer.cancel()
timeout = self.timeout * 60 if self.timeout is not None else None

if timed_out:
raise TimeoutError("Enumeration took too long")

else:
output = process.communicate()[0].decode("utf-8")
try:
output = process.communicate(timeout=timeout)[0].decode("utf-8")
except subprocess.TimeoutExpired as exc:
raise TimeoutError("Enumeration took too long") from exc

count = 0
start_count = False
Expand Down

0 comments on commit 6947ffc

Please sign in to comment.