当前位置:Gxlcms > Python > Python多线程中阻塞(join)与锁(Lock)使用误区解析

Python多线程中阻塞(join)与锁(Lock)使用误区解析

时间:2021-07-01 10:21:17 帮助过:82人阅读

这篇文章主要为大家详细 介绍了Python多线程中阻塞join与锁Lock的使用误区,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

关于阻塞主线程

join的错误用法

Thread.join() 作用为阻塞主线程,即在子线程未返回的时候,主线程等待其返回然后再继续执行.

join不能与start在循环里连用
以下为错误代码,代码创建了5个线程,然后用一个循环激活线程,激活之后令其阻塞主线程.

  1. threads = [Thread() for i in range(5)]
  2. for thread in threads:
  3. thread.start()
  4. thread.join()

执行过程:

1. 第一次循环中,主线程通过start函数激活线程1,线程1进行计算.
2. 由于start函数不阻塞主线程,在线程1进行运算的同时,主线程向下执行join函数.
3. 执行join之后,主线程被线程1阻塞,在线程1返回结果之前,主线程无法执行下一轮循环.
4. 线程1计算完成之后,解除对主线程的阻塞.
5. 主线程进入下一轮循环,激活线程2并被其阻塞…

如此往复,可以看出,本来应该并发的五个线程,在这里变成了顺序队列,效率和单线程无异.

join的正确用法

使用两个循环分别处理startjoin函数.即可实现并发.

  1. threads = [Thread() for i in range(5)]
  2. for thread in threads:
  3. thread.start()
  4. for thread in threads:
  5. thread.join()

time.sleep代替join进行调试

之前在一些项目里看到过这样的代码,使用time.sleep代替join手动阻塞主线程.
在所有子线程返回之前,主线程陷入无线循环而不能退出.

  1. for thread in threads:
  2. thread.start()
  3. while 1:
  4. if thread_num == 0:
  5. break
  6. time.sleep(0.01)

关于线程锁(threading.Lock)

单核CPU+PIL是否还需要锁?

非原子操作 count = count + 1 理论上是线程不安全的.
使用3个线程同时执行上述操作改变全局变量count的值,并查看程序执行结果.
如果结果正确,则表示未出现线程冲突.

使用以下代码测试

  1. # -*- coding: utf-8 -*-
  2. import threading
  3. import time
  4. count = 0
  5. class Counter(threading.Thread):
  6. def __init__(self, name):
  7. self.thread_name = name
  8. super(Counter, self).__init__(name=name)
  9. def run(self):
  10. global count
  11. for i in xrange(100000):
  12. count = count + 1
  13. counters = [Counter('thread:%s' % i) for i in range(5)]
  14. for counter in counters:
  15. counter.start()
  16. time.sleep(5)
  17. print 'count=%s' % count

运行结果:

count=275552

事实上每次运行结果都不相同且不正确,这证明单核CPU+PIL仍无法保证线程安全,需要加锁.

加锁后的正确代码:

  1. # -*- coding: utf-8 -*-
  2. import threading
  3. import time
  4. count = 0
  5. lock = threading.Lock()
  6. class Counter(threading.Thread):
  7. def __init__(self, name):
  8. self.thread_name = name
  9. self.lock = threading.Lock()
  10. super(Counter, self).__init__(name=name)
  11. def run(self):
  12. global count
  13. global lock
  14. for i in xrange(100000):
  15. lock.acquire()
  16. count = count + 1
  17. lock.release()
  18. counters = [Counter('thread:%s' % i) for i in range(5)]
  19. for counter in counters:
  20. counter.start()
  21. time.sleep(5)
  22. print 'count=%s' % count

结果:

count=500000

注意锁的全局性

这是一个简单的Python语法问题,但在逻辑复杂时有可能被忽略.
要保证锁对于多个子线程来说是共用的,即不要在Thread的子类内部创建锁.

以下为错误代码

  1. # -*- coding: utf-8 -*-
  2. import threading
  3. import time
  4. count = 0
  5. # lock = threading.Lock() # 正确的声明位置
  6. class Counter(threading.Thread):
  7. def __init__(self, name):
  8. self.thread_name = name
  9. self.lock = threading.Lock() # 错误的声明位置
  10. super(Counter, self).__init__(name=name)
  11. def run(self):
  12. global count
  13. for i in xrange(100000):
  14. self.lock.acquire()
  15. count = count + 1
  16. self.lock.release()
  17. counters = [Counter('thread:%s' % i) for i in range(5)]
  18. for counter in counters:
  19. print counter.thread_name
  20. counter.start()
  21. time.sleep(5)
  22. print 'count=%s' % count

相关推荐:

python线程中同步锁详解

python多线程之事件Event的使用详解

python线程池threadpool的实现

以上就是Python多线程中阻塞(join)与锁(Lock)使用误区解析的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行