Synchronize stdout access to avoid multiple prints on 1 line : Thread Synchronize « Thread « Python






Synchronize stdout access to avoid multiple prints on 1 line

Synchronize stdout access to avoid multiple prints on 1 line


import thread

def counter(myId, count):
    
    for i in range(count): 
        mutex.acquire()
        print '[%s] => %s' % (myId, i)
        mutex.release()

mutex = thread.allocate_lock()
for i in range(10):
    thread.start_new(counter, (i, 100))

import time
time.sleep(10) 
print 'Main thread exiting.'

           
       








Related examples in the same category

1.Uses simple shared global data to know when threads are done in parent/main threadUses simple shared global data to know when threads are done in parent/main thread
2.Serialized by the mutex lockSerialized by the mutex lock