From 777c161c22c5b569642c128a42f739c6fb192d86 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 27 Feb 2014 14:00:13 +0100 Subject: add async and spawn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/util.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/util.py b/src/util.py index 0c44cdb..936538d 100644 --- a/src/util.py +++ b/src/util.py @@ -16,6 +16,32 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ''' +import sys +import threading +import subprocess -# TODO add utility functions + +def async(target, name = None, group = None): + ''' + Start a function asynchronously + + @param target:()→void The function + @param name:str? The name of the thread + @return :Thread A running deamon-thread running `target`, with the name `name` + ''' + t = threading.Thread(target = target, name = name) + t.setDaemon(True) + t.start() + return t + + +def spawn(*command): + ''' + Spawn an external process + + @param command:*str The command line + @return :istream The process's stdout + ''' + proc = process.Popen(list(command), stderr = sys.stderr, stdout = PIPE) + return proc.stdout -- cgit v1.2.3-70-g09d2