#!/usr/bin/env python import empty import thr import cmd_ut import yaml_ut import base import dbg thr_gc = thr.gc_new() def ecmd_new(cmd, id, que): proc = cmd_ut.proc_new( cmd, stdout=cmd_ut.PIPE ) que.put( 'start cmd ' + cmd ) f = proc.get_stdout() def stop(): proc.kill() proc.wait() que.put( 'stop cmd ' + cmd ) def th_f(): while True: s = f.readline().decode() if not s: break s = s.strip() if s: que.put( id + ': ' + s ) thr.th_new( th_f, gc=thr_gc ).start() return empty.new( locals() ) def run(): data_fn = 'cmds_out.yaml' fn = base.find_path( data_fn, __file__ ) if not fn: dbg.err_exit( data_fn + ' ?' ) d = yaml_ut.load_fn( fn ) lst = d.get( 'cmds', [] ) rdic = d.get( 'replace', {} ) th_show = thr.loop_que_new( dbg.out, 1.0, gc=thr_gc ) th_show.start() ecmds = [] for d in lst: o = empty.new( d, cmd='', id='' ) if o.cmd: o.cmd = base.str_replace_dic( o.cmd, rdic ) ecmd = ecmd_new( o.cmd, o.id, th_show.que ) ecmds.append( ecmd ) sigint = thr.sigint_new() sigint.wait() sigint.fini() for ecmd in ecmds: ecmd.stop() th_show.stop() thr_gc.stop() if __name__ == "__main__": run() # EOF