Python线程锁

线程锁

当一个数据有多个线程都可以对其进行修改的时候,任何一个线程改变它都会对其他线程造成影响,如果我们某一个线程在使用完之前,其他线程不能对其修改,就需要对这个线程增加一个线程锁。

lock

lock注重的是局部,某一个变量没有调用完,其他线程不能调用。

import threading
import time
import random
count = 0
def get_money(money):
    global count
    count += money
    count += money
    count -= money
lock = threading.Lock()
def lock_thread(money):
    # 加锁
    lock.acquire()
    time.sleep(random.randint(1, 3))
    print('当前线程为',threading.current_thread().name)
    get_money(money)
    time.sleep(random.randint(1,3))
    print('当前线程为', threading.current_thread().name)
    # 解锁
    lock.release()
thread1 = threading.Thread(target=lock_thread,name='thread1',args=(10000,))
thread2 = threading.Thread(target=lock_thread,name='thread2',args=(15000,))
thread1.start()
thread2.start()
print('hello world')

输出结果为:

hello world
当前线程为 thread1
当前线程为 thread1
当前线程为 thread2
当前线程为 thread2

join

join注重的是整体,线程1没有执行完,线程2不能执行。

import threading
import time
import random
count = 0
def get_money(money):
    global count
    count += money
    count += money
    count -= money
def lock_thread(money):
    time.sleep(random.randint(1, 3))
    print('当前线程为',threading.current_thread().name)
    get_money(money)
    time.sleep(random.randint(1,3))
    print('当前线程为', threading.current_thread().name)
thread1 = threading.Thread(target=lock_thread,name='thread1',args=(10000,))
thread2 = threading.Thread(target=lock_thread,name='thread2',args=(15000,))
thread1.start()

thread1.join()

thread2.start()
print('hello world')

执行结果为:

当前线程为 thread1
当前线程为 thread1
hello world
当前线程为 thread2
当前线程为 thread2

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,线程锁是一种用于同步多个线程对共享资源的访问的机制。通过使用线程锁,我们可以确保在某一时刻只有一个线程可以访问共享资源,从而避免多个线程同时对共享资源进行修改而导致的数据竞争问题。Python提供了多种线程锁的实现方式。 其中,threading模块提供了Lock类,可以使用threading.Lock()来创建一个线程锁对象。这个对象可以在需要访问共享资源的代码段前后调用acquire()和release()方法来上锁和解锁。当一个线程上锁后,其他线程需要等待直到锁被释放。 另外,Python还提供了RLock类(可重入锁),即threading.RLock(),它允许同一线程在没有释放其所有权的情况下多次获取同一个锁。这在某些情况下非常有用,比如在递归函数或嵌套调用中需要多次获取锁。 需要注意的是,在Python中,由于全局解释器锁(GIL)的存在,同一时刻只允许一个线程执行Python字节码,因此Python的多线程并不能实现真正的并行计算。如果需要进行并行计算,可以考虑使用multiprocessing模块或其他并行计算框架,如concurrent.futures。 综上所述,Python线程锁是一种用于同步多个线程对共享资源的访问的机制,通过使用Lock类或RLock类可以实现对共享资源的安全访问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python:创建线程、多线程的加锁、解锁机制](https://blog.csdn.net/weixin_44775255/article/details/120435611)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【python多线程中的锁你知道几种?】](https://blog.csdn.net/weixin_41777118/article/details/130416802)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值