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

Commit

Permalink
sends the child process streams to /dev/null
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekziade committed Aug 20, 2013
1 parent 70f4ae8 commit 3970ac1
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion loads/runners/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
import subprocess
import sys

import zmq
from zmq.eventloop import ioloop, zmqstream
Expand Down Expand Up @@ -181,6 +182,22 @@ def _execute(self):

self._loop.start()

def _null_streams(self, streams):
devnull = os.open(os.devnull, os.O_RDWR)
try:
for stream in streams:
if not hasattr(stream, 'fileno'):
# we're probably dealing with a file-like
continue
try:
stream.flush()
os.dup2(devnull, stream.fileno())
except IOError:
# some streams, like stdin - might be already closed.
pass
finally:
os.close(devnull)

def spawn_external_runner(self):
"""Spawns an external runner with the given arguments.
Expand Down Expand Up @@ -210,11 +227,16 @@ def spawn_external_runner(self):
env['LOADS_ZMQ_RECEIVER'] = self._receiver_socket
env['LOADS_RUN_ID'] = self.args.get('run_id', '')

def preexec_fn():
self._null_streams([sys.stdout, sys.stderr, sys.stdin])
os.setsid()

cmd_args = {
'env': env,
'stdout': subprocess.PIPE, # To silent the output.
'preexec_fn': preexec_fn,
'cwd': self.args.get('test_dir'),
}

self._processes.append(subprocess.Popen(cmd.split(' '), **cmd_args))

def stop_run(self):
Expand Down

0 comments on commit 3970ac1

Please sign in to comment.