1. Thread
1.1. Capturer les sorties stdout stderr (print)
Il faut redéfinir sys.stdout et sys.stderr pour récupérer les flux suivant le thread en cours
class OutputThread:
def __init__(self):
self.pidout = {}
self.current_thread = thread.get_ident()
def write(self, ch):
thread_id = thread.get_ident()
if thread_id == self.current_thread:
sys.__stdout__.write(str(ch))
if not self.pidout.has_key(thread_id):
self.pidout[thread_id] = cStringIO.StringIO()
self.pidout[thread_id].write(ch)
def send(self, stdout):
thread_id = thread.get_ident()
if self.pidout.has_key(thread_id):
stdout.write(self.get_data())
self.close_stdout()
def get_data(self):
thread_id = thread.get_ident()
if self.pidout.has_key(thread_id):
return self.pidout[thread_id].getvalue()
def close_stdout(self):
thread_id = thread.get_ident()
if self.pidout.has_key(thread_id):
del self.pidout[thread_id]
stdout.flush()
stdout.close()
sys.stdout = sys.stderr = OutputThread