Skip to content

Commit

Permalink
1220 added timeout for process running
Browse files Browse the repository at this point in the history
  • Loading branch information
tian50 committed Dec 20, 2020
1 parent e96954c commit f7b5f0f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
15 changes: 8 additions & 7 deletions presg.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def preSG1D(xr_sg, smdim):
return sg


def preSG(sg_xml, analysis, ppcmd='prevabs', solver='swiftcomp', write_input=True, scrnout=True):
def preSG(sg_xml, analysis, ppcmd='prevabs', solver='swiftcomp', write_input=True, timeout=30, scrnout=True):
""" Preprocessor of a structure gene.
Parameters
Expand Down Expand Up @@ -331,12 +331,13 @@ def preSG(sg_xml, analysis, ppcmd='prevabs', solver='swiftcomp', write_input=Tru
cmd.append('-' + analysis)

# cmd = ' '.join(cmd)
if scrnout:
print('cmd:', cmd)
sbp.call(cmd)
else:
FNULL = open(os.devnull, 'w')
sbp.call(cmd, stdout=FNULL, stderr=sbp.STDOUT)
utl.run(cmd, timeout, scrnout)
# if scrnout:
# print('cmd:', cmd)
# sbp.call(cmd)
# else:
# FNULL = open(os.devnull, 'w')
# sbp.call(cmd, stdout=FNULL, stderr=sbp.STDOUT)

return fn_sg_in, smdim

Expand Down
21 changes: 20 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import os
import sys
import math
import subprocess as sbp
import numpy as np
# import pandas as pd
import xml.etree.ElementTree as et
import msgpi.sg as sgm


def run(cmd, timeout, scrnout):
# proc = sbp.Popen(cmd, stdout=sbp.PIPE, stderr=sbp.PIPE)
if scrnout:
# sbp.call(cmd)
proc = sbp.Popen(cmd)
else:
FNULL = open(os.devnull, 'w')
# sbp.call(cmd, stdout=FNULL, stderr=sbp.STDOUT)
proc = sbp.Popen(cmd, stdout=FNULL, stderr=sbp.STDOUT)

try:
stdout, stderr = proc.communicate(timeout=timeout)
except sbp.TimeoutExpired as e:
print('TimeoutExpired')
proc.kill()
print('Process killed')


def tilde(v):
"""
~v_{ij} = -e_{ijk} * v_k
Expand Down

0 comments on commit f7b5f0f

Please sign in to comment.