Commit 82d8ca0f authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

[Python] ServerManager is now available in hpp-corbaserver

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