Proper use of mutexes in Python Proper use of mutexes in Python python python

Proper use of mutexes in Python


I don't know why you're using the Window's Mutex instead of Python's. Using the Python methods, this is pretty simple:

from threading import Thread, Lockmutex = Lock()def processData(data):    mutex.acquire()    try:        print('Do some stuff')    finally:        mutex.release()while True:    t = Thread(target = processData, args = (some_data,))    t.start()

But note, because of the architecture of CPython (namely the Global Interpreter Lock) you'll effectively only have one thread running at a time anyway--this is fine if a number of them are I/O bound, although you'll want to release the lock as much as possible so the I/O bound thread doesn't block other threads from running.

An alternative, for Python 2.6 and later, is to use Python's multiprocessing package. It mirrors the threading package, but will create entirely new processes which can run simultaneously. It's trivial to update your example:

from multiprocessing import Process, Lockmutex = Lock()def processData(data):    with mutex:        print('Do some stuff')if __name__ == '__main__':    while True:        p = Process(target = processData, args = (some_data,))        p.start()


This is the solution I came up with:

import timefrom threading import Threadfrom threading import Lockdef myfunc(i, mutex):    mutex.acquire(1)    time.sleep(1)    print "Thread: %d" %i    mutex.release()mutex = Lock()for i in range(0,10):    t = Thread(target=myfunc, args=(i,mutex))    t.start()    print "main loop %d" %i

Output:

main loop 0main loop 1main loop 2main loop 3main loop 4main loop 5main loop 6main loop 7main loop 8main loop 9Thread: 0Thread: 1Thread: 2Thread: 3Thread: 4Thread: 5Thread: 6Thread: 7Thread: 8Thread: 9


I would like to improve answer from chris-ba little bit more.

See below for my code:

from threading import Thread, Lockimport threadingmutex = Lock()def processData(data, thread_safe):    if thread_safe:        mutex.acquire()    try:        thread_id = threading.get_ident()        print('\nProcessing data:', data, "ThreadId:", thread_id)    finally:        if thread_safe:            mutex.release()counter = 0max_run = 100thread_safe = Falsewhile True:    some_data = counter            t = Thread(target=processData, args=(some_data, thread_safe))    t.start()    counter = counter + 1    if counter >= max_run:        break

In your first run if you set thread_safe = False in while loop, mutex will not be used, and threads will step over each others in print method as below;

Not Thread safe

but, if you set thread_safe = True and run it, you will see all the output comes perfectly fine;

Thread safe

hope this helps.