Proper use of mutexes in Python

前端 未结 4 2003
北荒
北荒 2020-12-04 07:04

I am starting with multi-threads in python (or at least it is possible that my script creates multiple threads). would this algorithm be the right usage of a Mutex? I haven\

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 07:53

    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, Lock
    
    mutex = 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, Lock
    
    mutex = Lock()
    
    def processData(data):
        with mutex:
            print('Do some stuff')
    
    if __name__ == '__main__':
        while True:
            p = Process(target = processData, args = (some_data,))
            p.start()
    

提交回复
热议问题