Skip to content
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

Subprocess shell #104

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 27 additions & 4 deletions bin/user/sdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ def __init__(self):
self.stdout_reader = None
self.stderr_queue = queue.Queue()
self.stderr_reader = None
self.uses_shell = None

def startup(self, cmd, path=None, ld_library_path=None):
def startup(self, cmd, path=None, use_shell=None, ld_library_path=None):
self._cmd = cmd
loginf("startup process '%s'" % self._cmd)
env = os.environ.copy()
Expand All @@ -225,7 +226,15 @@ def startup(self, cmd, path=None, ld_library_path=None):
if ld_library_path:
env['LD_LIBRARY_PATH'] = ld_library_path
try:
self._process = subprocess.Popen(cmd.split(' '),
if use_shell:
self.uses_shell = True
self._process = subprocess.Popen(cmd,
shell=True,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
else:
self._process = subprocess.Popen(cmd.split(' '),
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Expand All @@ -241,7 +250,20 @@ def startup(self, cmd, path=None, ld_library_path=None):

def shutdown(self):
loginf('shutdown process %s' % self._cmd)
self._process.kill()
logdbg('waiting for %s' % self.stdout_reader.getName())
self.stdout_reader.stop_running()
if self.uses_shell:
os.kill(self._process.pid, signal.SIGTERM)
self.stdout_reader.join(10.0)
if self.stdout_reader.isAlive():
loginf('timed out waiting for %s' % self.stdout_reader.getName())
self.stdout_reader = None
logdbg('waiting for %s' % self.stderr_reader.getName())
self.stderr_reader.stop_running()
self.stderr_reader.join(10.0)
if self.stderr_reader.isAlive():
loginf('timed out waiting for %s' % self.stderr_reader.getName())
self.stderr_reader = None
logdbg("close stdout")
self._process.stdout.close()
logdbg("close stderr")
Expand Down Expand Up @@ -3279,10 +3301,11 @@ def __init__(self, **stn_dict):
self._counter_values = dict()
cmd = stn_dict.get('cmd', DEFAULT_CMD)
path = stn_dict.get('path', None)
use_shell = stn_dict.get('use_shell', None)
ld_library_path = stn_dict.get('ld_library_path', None)
self._last_pkt = None # avoid duplicate sequential packets
self._mgr = ProcManager()
self._mgr.startup(cmd, path, ld_library_path)
self._mgr.startup(cmd, path, use_shell, ld_library_path)

def closePort(self):
self._mgr.shutdown()
Expand Down