salve a tutti, sto lavorando su uno script che lancia su piu' client un comando. per il momento sto usando subprocess, ma volevo lanciare i processi in parallelo e ricavare l'output mano a mano che i processi terminano.
tenete conto che lo script va lanciato da cron, e mi serve l'output del comando per inviarlo via mail. dopo varie prove sono arrivato a questo, ma mi chiedevo se fosse il modo giusto (cioe' l'utilizzo del contatore _act) per aspettare che tutti i processi terminino e per leggere dalla queue l'output. grazie in anticipo: [CODE] #!/usr/bin/env python2.7 # import subprocess as sp import threading as thr try: import queue except ImportError: import Queue as queue def run_command(qq, args): try: _cmd = list(args) _userid = _cmd[1] _proc = sp.Popen(_cmd, stdout=sp.PIPE, stderr=sp.PIPE) (_stdout, _stderr) = _proc.communicate() qq.put(' *** %s ***\n\n[STDOUT]: %s\n[STDERR]: %s\n\n *** END ***\n\n' % ( _userid, _stdout, _stderr,)) except Exception as err: log.error('errore %s: %s' % (_userid, str(err))) qq.put(' *** %s ***\n\n[ERRORE]: %s\n\n *** END ***\n\n' % ( _userid, str(err),)) finally: qq.task_done() def run_unison(clients, qq, args): _act = 0 for k, v in clients.items(): for scr in v['server']: _cmd = [ 'ssh', '%s@%s' % (k, v['host'],), '"/Users/%s/bin/%s_%s.command"' % (k, k, scr,) ] + _extra_params t = thr.Thread(target=run_command, args=(qq, _cmd)) t.start() _act += 1 print(' ') while _act > 0: _out_qq = qq.get() print(_out_qq.decode('utf8')) _act -= 1 if __name__ == '__main__': qq = queue.Queue() run_unison(_cli, qq, args) [CODE] -- Never try to teach a pig to sing. It wastes your time and annoys the pig. _______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python