Skip to content
Snippets Groups Projects
Commit 82d8ca0f authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

[Python] ServerManager is now available in hpp-corbaserver

parent 3431a1e1
No related branches found
No related tags found
No related merge requests found
......@@ -4,35 +4,41 @@
import os
import subprocess
import time
import warnings
try:
from subprocess import DEVNULL, run
except ImportError: # Python2 fallback
DEVNULL = os.open(os.devnull, os.O_RDWR)
def run(*args):
subprocess.Popen(*args).wait()
class ServerManager:
"""A context to ensure a server is running."""
def __init__(self, server):
self.server = server
run(['killall', self.server])
def __enter__(self):
"""Run the server in background
stdout and stderr outputs of the child process are redirected to devnull (hidden).
preexec_fn is used to ignore ctrl-c signal send to the main script
(otherwise they are forwarded to the child process)
"""
self.process = subprocess.Popen(self.server,
stdout=DEVNULL,
stderr=DEVNULL,
preexec_fn=os.setpgrp)
# give it some time to start
time.sleep(3)
def __exit__(self, exc_type, exc_value, exc_traceback):
self.process.kill()
from hpp.utils import ServerManager
warnings.warn('Please import ServerManager directly from hpp.utils', DeprecationWarning)
except ImportError: # hpp-corbaserver < 4.9.1 fallback
try:
from subprocess import DEVNULL, run
except ImportError: # Python2 fallback
DEVNULL = os.open(os.devnull, os.O_RDWR)
def run(*args):
subprocess.Popen(*args).wait()
class ServerManager:
"""A context to ensure a server is running."""
def __init__(self, server):
self.server = server
run(['killall', self.server])
def __enter__(self):
"""Run the server in background
stdout and stderr outputs of the child process are redirected to devnull (hidden).
preexec_fn is used to ignore ctrl-c signal send to the main script
(otherwise they are forwarded to the child process)
"""
self.process = subprocess.Popen(self.server,
stdout=DEVNULL,
stderr=DEVNULL,
preexec_fn=os.setpgrp)
# give it some time to start
time.sleep(3)
def __exit__(self, exc_type, exc_value, exc_traceback):
self.process.kill()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment