Python从门到精通(六):线程-04-锁(python线程锁的用法)

网友投稿 313 2022-08-27


Python从门到精通(六):线程-04-锁(python线程锁的用法)

一、加锁

1.1、Lock

import threadingclass SharedCounter: ''' A counter object that can be shared by multiple threads. ''' def __init__(self, initial_value = 0): self._value = initial_value self._value_lock = threading.Lock() def inc_r(self,delta=1): ''' Increment the counter with locking ''' with self._value_lock: self._value += delta def dec_r(self,delta=1): ''' Decrement the counter with locking ''' with self._value_lock: self._value -= delta

1.2、显示锁

import threadingclass SharedCounter: ''' A counter object that can be shared by multiple threads. ''' def __init__(self, initial_value = 0): self._value = initial_value self._value_lock = threading.Lock() def inc_r(self,delta=1): ''' Increment the counter with locking ''' self._value_lock.acquire() self._value += delta self._value_lock.release() def dec_r(self,delta=1): ''' Decrement the counter with locking ''' self._value_lock.acquire() self._value -= delta self._value_lock.release()

1.3、可重入锁

import threadingclass SharedCounter: ''' A counter object that can be shared by multiple threads. ''' _lock = threading.RLock() def __init__(self, initial_value = 0): self._value = initial_value def inc_r(self,delta=1): ''' Increment the counter with locking ''' with SharedCounter._lock: self._value += delta def dec_r(self,delta=1): ''' Decrement the counter with locking ''' with SharedCounter._lock: self.inc_r(-delta)

1.4、并发锁

from threading import Semaphoreimport urllib.request# At most, five threads allowed to run at once_fetch_url_sema = Semaphore(5)def fetch_url(url): with _fetch_url_sema: return urllib.request.urlopen(url)

二、死锁

2.1、上下文管理器

import threadingfrom contextlib import contextmanager_local = threading.local()@contextmanagerdef acquire(*locks): # Sort locks by object identifier locks = sorted(locks, key=lambda x: id(x)) # Make sure lock order of previously acquired locks is not violated acquired = getattr(_local,'acquired',[]) if acquired and max(id(lock) for lock in acquired) >= id(locks[0]): raise RuntimeError('Lock Order Violation') # Acquire all of the locks acquired.extend(locks) _local.acquired = acquired try: for lock in locks: lock.acquire() yield finally: # Release locks in reverse order of acquisition for lock in reversed(locks): lock.release() del acquired[-len(locks):]

2.2、申请锁

import threadingfrom chapter12.avoid_lock_1 import acquirex_lock = threading.Lock()y_lock = threading.Lock()def thread_1(): while True: with acquire(x_lock, y_lock): print('Thread-1')def thread_2(): while True: with acquire(y_lock, x_lock): print('Thread-2')t1 = threading.Thread(target=thread_1)t1.daemon = Truet1.start()t2 = threading.Thread(target=thread_2)t2.daemon = Truet2.start()

2.3、TLS检测锁

import threadingfrom chapter12.avoid_lock_1 import acquirex_lock = threading.Lock()y_lock = threading.Lock()def thread_1(): while True: with acquire(x_lock): with acquire(y_lock): print('Thread-1')def thread_2(): while True: with acquire(y_lock): with acquire(x_lock): print('Thread-2')t1 = threading.Thread(target=thread_1)t1.daemon = Truet1.start()t2 = threading.Thread(target=thread_2)t2.daemon = Truet2.start()

import threadingfrom chapter12.avoid_lock_1 import acquire# The philosopher threaddef philosopher(left, right): while True: with acquire(left,right): print(f'{threading.currentThread()} eating')# The chopsticks (represented by locks)NSTICKS = 5chopsticks = [threading.Lock() for n in range(NSTICKS)]# Create all of the philosophersfor n in range(NSTICKS): t = threading.Thread(target=philosopher, args=(chopsticks[n],chopsticks[(n+1) % NSTICKS])) t.start()


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python ❀ 文件与异常(python怎么读)
下一篇:springboot集成spark并使用spark
相关文章

 发表评论

暂时没有评论,来抢沙发吧~