Print logs to standard output in a multithreaded manner
#!/usr/bin/pythonImport TimeImportThreadingclassPrintthread (Threading. Thread):def __init__(Self,threadid,count,mutex): Threading. Thread.__init__(self) self.threadid=ThreadID Self.count=Count Self.mutex=MutexdefRun (self): with Self.mutex: forIteminchRange (Self.count): Time.sleep (0.05) Print 'ThreadID is {0} This is the count {1}'. Format (self.threadid,item)classPrintThread2 (Threading. Thread):def __init__(Self,threadid,count,mutex): Threading. Thread.__init__(self) self.threadid=ThreadID Self.count=Count Self.mutex=MutexdefRun (self): forIteminchRange (Self.count): With Self.mutex:time.sleep (0.1) Print 'thread ID is {0} This is the count {1}'. Format (self.threadid,item)if __name__=="__main__": Threads=[] Stdoutlock=Threading. Lock () forIteminchRange (5): PT=printthread2 (item,100, Stdoutlock) Threads.append (PT) Pt.start () forIteminchThreads:item.join ()Print 'This end ...'
Python threading module for multithreading