Added run_in_background function.

This commit is contained in:
Lukáš Mandák 2015-07-14 16:14:29 +02:00
parent e85425c8c6
commit 6f2c8f2e40

View File

@ -1,4 +1,4 @@
# from subprocess import CalledProcessError import logging
from collections import namedtuple from collections import namedtuple
import subprocess import subprocess
@ -14,6 +14,9 @@ def run_through_shell(command, enable_shell=False):
in one variable in one variable
""" """
if not enable_shell and not isinstance(command, list):
command = command.split()
returncode = None returncode = None
stderr = None stderr = None
try: try:
@ -27,7 +30,35 @@ def run_through_shell(command, enable_shell=False):
except OSError as e: except OSError as e:
out = e.strerror out = e.strerror
stderr = e.strerror stderr = e.strerror
logging.getLogger("i3pystatus.command").exception("")
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
out = e.output out = e.output
logging.getLogger("i3pystatus.command").exception("")
return CommandResult(returncode, out, stderr) return CommandResult(returncode, out, stderr)
def run_in_background(command, detach=False):
"""
Runs a command in background.
No output is retrieved.
Useful for running GUI applications that would block click events.
:param detach If set to `True` the application will be executed using the
`i3-msg` command.
"""
if not isinstance(command, list):
command = command.split()
if detach:
command.insert(0, "exec")
command.insert(0, "i3-msg")
try:
subprocess.Popen(command, stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except OSError:
logging.getLogger("i3pystatus.command").exception("")
except subprocess.CalledProcessError:
logging.getLogger("i3pystatus.command").exception("")