From 0f9b0a1ac30a37d103ad3db23244b6c6939931f3 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Wed, 26 Oct 2016 21:42:40 -0200 Subject: [PATCH] Use context manager in Popen --- i3pystatus/redshift.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/i3pystatus/redshift.py b/i3pystatus/redshift.py index fc90724..8c03870 100644 --- a/i3pystatus/redshift.py +++ b/i3pystatus/redshift.py @@ -32,7 +32,14 @@ class RedshiftController(threading.Thread): env = os.environ.copy() env['LANG'] = env['LANGUAGE'] = env['LC_ALL'] = env['LC_MESSAGES'] = 'C' - self._p = Popen(cmd, env=env, stdout=PIPE, bufsize=1, universal_newlines=True) + + self._params = { + "args": cmd, + "env": env, + "bufsize": 1, + "stdout": PIPE, + "universal_newlines": True, + } def parse_output(self, line): """Convert output to key value pairs""" @@ -96,11 +103,10 @@ class RedshiftController(threading.Thread): self._inhibited = inhibit def run(self): - for line in self._p.stdout: - self.parse_output(line) - - self._p.stdout.close() - self._p.wait(10) + with Popen(**self._params) as proc: + for line in proc.stdout: + self.parse_output(line) + proc.wait(10) class Redshift(IntervalModule):