python 包之 threading 多线程

作者:autofelix 时间:2021-06-03 19:54:41 

一、创建一个线程

  • 通过实例化threading.Thread类创建线程

import threading

def func(s):
print(s)

if __name__ == '__main__':
# 创建线程
thread = threading.Thread(target=func, args=('hello',))
# 启动线程
thread.start()
# 等待线程结束
thread.join()

二、创建多个线程

import threading

def func(s):
print(s)

if __name__ == '__main__':
thread = [
threading.Thread(target=func, args=('1', ))
threading.Thread(target=func, args=('2', ))
]

[t.start() for t in thread]
[t.join() for t in thread]

三、线程同步

  • 使用锁实现线程同步

  • threading.Lock是直接通过_thread模块扩展实现的

  • 锁只有“锁定”和“非锁定”两种状态

  • 同一个线程获取锁后,如果在释放锁之前再次获取锁会导致当前线程阻塞,除非有另外的线程来释放锁,如果只有一个线程,并且发生了这种情况,会导致这个线程一直阻塞下去,即形成了死锁。

import time
import threading

# 创建锁
lock = threading.Lock()
# 全局变量
global_resource = [None] * 5

def change_resource(para, sleep):
# 请求锁
lock.acquire()
# 这段代码如果不加锁,第一个线程运行结束后global_resource中是乱的,输出为:结果是: ['hello', 'hi', 'hi', 'hello', 'hello']
# 第二个线程运行结束后,global_resource中还是乱的,输出为:结果是: ['hello', 'hi', 'hi', 'hi', 'hi']
global global_resource
for i in range(len(global_resource)):
global_resource[i] = para
time.sleep(sleep)
print("结果是:", global_resource)

# 释放锁
lock.release()

if __name__ == '__main__':
thread = [
threading.Thread(target=change_resource, args=('hi', 2))
threading.Thread(target=change_resource, args=('hello', 1))
]

[t.start() for t in thread]
[t.join() for t in thread]

# 结果是: ['hi', 'hi', 'hi', 'hi', 'hi']
# 结果是: ['hello', 'hello', 'hello', 'hello', 'hello']

四、递归锁

  • 上面线程同步使用的是普通锁,也就是只有锁的状态,并不知道是哪个线程加的锁

  • 这样的话使用普通锁时,对于一些可能造成死锁的情况,可以考虑使用递归锁来解决

  • 递归锁和普通锁的差别在于加入了“所属线程”和“递归等级”的概念

  • 释放锁必须有获取锁的线程来进行释放

import time
import threading

# 使用成一个递归锁就可以解决当前这种死锁情况
rlock_hi = rlock_hello = threading.RLock()

def test_thread_hi():
# 初始时锁内部的递归等级为1
rlock_hi.acquire()
print('线程test_thread_hi获得了锁rlock_hi')
time.sleep(2)
# 如果再次获取同样一把锁,则不会阻塞,只是内部的递归等级加1
rlock_hello.acquire()
print('线程test_thread_hi获得了锁rlock_hello')
# 释放一次锁,内部递归等级减1
rlock_hello.release()
# 这里再次减,当递归等级为0时,其他线程才可获取到此锁
rlock_hi.release()

def test_thread_hello():
rlock_hello.acquire()
print('线程test_thread_hello获得了锁rlock_hello')
time.sleep(2)
rlock_hi.acquire()
print('线程test_thread_hello获得了锁rlock_hi')
rlock_hi.release()
rlock_hello.release()

if __name__ == '__main__':
thread = [
threading.Thread(target=test_thread_hi)
threading.Thread(target=test_thread_hello)
]

[t.start() for t in thread]
[t.join() for t in thread]

五、信号锁

  • 一个信号量管理一个内部计数器

  • acquire()方法会减少计数器,release()方法则增加计数器

  • 计数器的值永远不会小于零

  • 当调用acquire()时,如果发现该计数器为零,则阻塞线程

  • 直到调用release()方法使计数器增加。

import time
import threading

# 创建信号量对象,初始化计数器值为3
semaphore3 = threading.Semaphore(3)

def thread_semaphore(index):
# 信号量计数器减1
semaphore3.acquire()
time.sleep(2)
print('thread_%s is running...' % index)
# 信号量计数器加1
semaphore3.release()

if __name__ == '__main__':
# 虽然会有9个线程运行,但是通过信号量控制同时只能有3个线程运行
# 第4个线程启动时,调用acquire发现计数器为0了,所以就会阻塞等待计数器大于0的时候
for index in range(9):
threading.Thread(target=thread_semaphore, args=(index, )).start()

来源:https://blog.51cto.com/autofelix/5167799

标签:python,包,threading,多线程
0
投稿

猜你喜欢

  • vue使用微信JS-SDK实现分享功能

    2023-07-02 16:59:46
  • SQL Server asp.net 数据提供程序连接池

    2009-09-18 08:18:00
  • python ChainMap管理用法实例讲解

    2022-05-01 19:02:22
  • python画图的函数用法以及技巧

    2021-11-01 17:58:19
  • MySQL事务与隔离级别的使用基础理论

    2024-01-23 20:14:49
  • go语言获取系统盘符的方法

    2024-05-22 10:19:47
  • JavaScript中的全局对象介绍

    2024-04-22 22:41:29
  • JavaScript lastIndexOf方法入门实例(计算指定字符在字符串中最后一次出现的位置)

    2024-04-18 10:54:17
  • Python编程之基于概率论的分类方法:朴素贝叶斯

    2023-10-08 10:15:52
  • 如何基于python对接钉钉并获取access_token

    2023-11-27 04:25:07
  • python的pyecharts绘制各种图表详细(附代码)

    2021-12-29 16:51:13
  • Python列表常用函数使用详解

    2021-08-21 01:52:05
  • Golang极简入门教程(二):方法和接口

    2024-04-25 15:16:48
  • xml xpath基础语法

    2008-01-21 12:46:00
  • 聊聊Python pandas 中loc函数的使用,及跟iloc的区别说明

    2023-08-11 09:11:36
  • 用python的哈希函数对密码加密

    2021-06-10 21:02:58
  • 利用Google Ajax Library API加速常用js类库的载入

    2008-06-17 17:44:00
  • Golang设计模式工厂模式实战写法示例详解

    2024-05-25 15:11:42
  • python3.9和pycharm的安装教程并创建简单项目的步骤

    2021-06-18 05:34:16
  • Python在字典中查找元素的3种方式

    2023-08-07 21:33:35
  • asp之家 网络编程 m.aspxhome.com