python thread模块如何实现多线程

399次阅读
没有评论

python

1、说明

python语言提供了两个与多线程相关的模块,一个是thread模块,另一个是threading模块。与threading模块相比,thread模块提供的功能和函数相对较少,只提供低水平的线程和简单的锁定,threading模块相对处理多线程的函数较多。

2、创建使用线程

1# 导入 thread 模块
2import thread
3# 创建使用新线程
4thread.start_new_thread ( func, args[, kwargs] )
5# 参数介绍
6func -- 线程要执行的函数
7args -- 传递给线程的参数,必须是元组类型
8kwargs -- 可选参数

3、线程同步

1# 导入 thread 模块
 2import thread
 3# 分配锁对象
 4lock_ = thread.allocate_lock()
 5# 获取锁对象
 6lock_.acquire()
 7# 释放锁对象
 8lock_.release()
 9# 查看锁状态
10lock_.locked()
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-26发表,共计445字。
新手QQ群:570568346,欢迎进群讨论 Python51学习