简体   繁体   中英

python thread lock doesn't lock the rest of thread

here is my code:

def inner_func(lock):
    with lock:
        print (f'{threading.current_thread().getName()} - inner_function - lock - acquire')
        print (f'{threading.current_thread().getName()} - inner_function - lock - processing')
        print (f'{threading.current_thread().getName()} - inner_function - lock - release')
    print(f'{threading.current_thread().getName()} - inner_function')

def outer_func(a, lock):
    inner_func(lock)
    print(f'{threading.current_thread().getName()} - outsider_function - input: {a}')

class Worker():
    def __init__(self, num_threads, input_item):
        self.t             = threading
        self.lock          = self.t.Lock()
        self.q             = queue.Queue()
        self.num_thread    = num_threads
        self.input_item    = input_item

    def worker(self):
        while True:
            item = self.q.get()
            if item:    item.append(self.lock)
            if not item:
                break
            outer_func(*item)
            self.q.task_done()

    def main(self):
        threads = []
        for _ in range(self.num_thread):
            t = self.t.Thread(target=self.worker)
            t.start()
            threads.append(t)

        for item in self.input_item:
            self.q.put(item)
        self.q.join()
        for _ in range(self.num_thread):
            self.q.put(None)
        for t in threads:
            t.join()

container = [['a'], ['b'], ['c'], ['d'], ['e'], ['f'], ['g']]
Worker(7, container).main()

I am trying to create inner_func which is used by outer_func to testify if threading.Lock() can pass to sub-function. After the run, it doesn't seem to me that the lock locks other threads, as the print message shows up other messages between my lock's message.

The print result as the following:

.......(other print)
Thread-6 - inner_function - lock - acquire
Thread-7 - outsider_function - input: c
Thread-6 - inner_function - lock - processing
Thread-6 - inner_function - lock - release
.......(other print)

As above indicated, Thread-6 already acquire the lock, but Thread-7 still active before the release of lock in Thread-6.

Does it mean the lock doesn't work? if not, how can I prove when the lock is active? or when other thread is not active during active of lock in single thread.

The following is another problem on print:

Thread-6 - outsider_function - input: dThread-1 - inner_function - lock - acquire

above should print in two different lines. how to fix it? thanks.

The prints outside the with lock: block are unordered with respect to the lock. If you want them ordered you need to wrap them in their own with lock: .

Consider what:

def inner_func(lock):
    with lock:
        print (f'{threading.current_thread().getName()} - inner_function - lock - acquire')
        print (f'{threading.current_thread().getName()} - inner_function - lock - processing')
        print (f'{threading.current_thread().getName()} - inner_function - lock - release')
    with lock:    
        print(f'{threading.current_thread().getName()} - inner_function')

def outer_func(a, lock):
    inner_func(lock)
    with lock:    
        print(f'{threading.current_thread().getName()} - outsider_function - input: {a}')

does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM