Python连载31-threading的使用
一、 例子:我们对传参是有要求的必须传入一个元组,否则报错
二、
- import _thread as thread
- import time
- def loop1(in1):
- print("Start loop 1 at:",time.ctime())
- print("我是参数",in1)
- time.sleep(4)
- print("End loop 1 at:",time.ctime())
- def loop2(in1,in2):
- print("Start loop 2 at:",time.ctime())
- print("我是参数",in1,"和参数 ",in2)
- time.sleep(4)
- print("End loop 1 at:",time.ctime())
- def main():
- print("Starting at:",time.ctime())
- thread.start_new_thread(loop1,("liuming",))
- #上面我们传参的时候,我用的是:(“liuming”),这里面是没有逗号的,结果编译报错,告诉我,这里面必须传入元组
- #因此,我才在里面加了一个逗号,使其变成一个元组
- thread.start_new_thread(loop2,("zhanglei","liuhao"))
- print("All done at:",time.ctime())
- if __name__ == "__main__":
- main()
- while True:
- time.sleep(10)
-
二、threading的使用
直接利用threading.Thread生成Thread的实例
格式:
t= threading.Thread(target=函数体,args=(,))#参数args要传递元组
t.start()#启动多线程
t.join()#等待多线程执行完成
- def main():
- print("Start at :",time.ctime())
- t1 = threading.Thread(target=loop1,args=("王老大",))
- t1.start()#启动多线程
- t2 = threading.Thread(target=loop2,args=("孙子","好吗"))
- t2.start()
- t1.join()
- t2.join()
- print("End at :",time.ctime())
- if __name__ == "__main__":
- main()
从上面可以看出来,我们启动了两个线程,但是这两个线程执行完了才打印最后一个结束语句。
2.守护线程
格式:线程.setDaemon(True)
作用:
(1)如果在程序中将子线程设置为守护线程,则子线程会在主线程结束的时候自动退出;
(2)一般认为,守护线程不重要或者不允许脱离子线程而独立运行;
(3)守护线程能否有效果和环境有关系
注意点:该语句一定要写在start语句之前,否则就会把子程序无限时间挂起,运行报错,如:
- def fun():
- print("Start fun")
- time.sleep(2)
- print("End fun")
-
- print('Main thread')
- t3 = threading.Thread(target=fun,args=())
- t3.setDaemon(True)
- t3.start()
- time.sleep(1)
- print("Main thread End")
解释:我们可以看出主线程结束后(即打印完了”Main thread End")后,我们的子线程的最后一个打印没有出来,程序就结束了,说明主线程结束,子线程无论执行到哪里都会被kill掉,和我们的预期一样。
三、源码
d24_2_usage_of_threading.py
地址:https://github.com/ruigege66/Python_learning/blob/master/d24_2_usage_of_threading.py
2.CSDN:https://blog.csdn.net/weixin_44630050(心悦君兮君不知-睿)
3.博客园:https://www.cnblogs.com/ruigege0000/
4.欢迎关注微信公众号:傅里叶变换,后台回复”礼包“,获取大数据学习资料
Python连载31-threading的使用的更多相关文章
- python基础31[常用模块介绍]
python基础31[常用模块介绍] python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的li ...
- python 线程之 threading(四)
python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
- Python中的threading
Python中的threading RLock--重入锁 RLock在Python中的实现是对Lock的封装,具体在类中维护了一个重入次数的变量.一旦一个线程获得一个RLock,该线程再次要求获得该锁 ...
- Python:使用threading模块实现多线程编程
转:http://blog.csdn.net/bravezhe/article/details/8585437 Python:使用threading模块实现多线程编程一[综述] Python这门解释性 ...
- Python多线程(threading模块)
线程(thread)是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务. ...
- Python连载13-shutile模块(续)和zipfile模块
一.shutil模块(续) 1.函数:upack_archive() (1)用法:解包操作 (2)格式:shutil.unpack_archive("归档文件地址“,”解包之后的地址“) ( ...
- Python连载12-shutil模块
一.shutil模块 1.函数:copy() (1)用法:复制文件0 (2)格式:copy(来源路径,目标路径) (3)返回值:返回目标路径 (4)注意:拷贝的同时可以给文件重命名 source_pa ...
- python标准库介绍——31 threading 模块详解
threading 模块 (可选) ``threading`` 模块为线程提供了一个高级接口, 如 [Example 3-1 #eg-3-1] 所示. 它源自 Java 的线程实现. 和低级的 ``t ...
- 【Python@Thread】threading模块
theading模块的Thread类 属性: name 线程名 ident 线程标识符 daemon 布尔值,标示是否为守护线程 方法: __init__(target=None, name=Non ...
随机推荐
- go 1.11 模块和版本管理
自2007年“三巨头(Robert Griesemer, Rob Pike, Ken Thompson)”提出设计和实现Go语言以来,Go语言已经发展和演化了十余年了.这十余年来,Go取得了巨大的成就 ...
- Python - 字符串 - 第七天
Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hel ...
- JQuery 遍历table中的checkbox 并对行数据进行校验
JQuery中confirm的使用 $(document).ready(function () { $("#Btn_Print").click(function () { var ...
- Java学习——注解
Java学习——注解 摘要:本文主要介绍了Java开发常用的注解,以及如何自定义注解. 部分内容来自以下博客: https://www.cnblogs.com/Qian123/p/5256084.ht ...
- Android Studio Gradle被墙bug总结
1 Unknown host 'd29vzk4ow07wi7.cloudfront.net'. You may need to adjust the proxy settings in Gradle ...
- LeetCode——Department Highest Salary(花式使用IN以及GROUP BY)
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- maven的配置及基本操作
---恢复内容开始--- 1.官网下载maven 官方网址:http://maven.aparche.org 2.将maven解压到硬盘(最好没有中文路径)下 3.配置maven环境变量 4.配置m ...
- 在VideoFileClip函数中获取“OSError:[WinError 6]句柄无效”
我正在使用python通过导入moviepy库创建一个程序,但收到以下错误: from moviepy.editor import VideoFileClip white_output = 'vide ...
- 以太网PHY寄存器分析【转】
转自:https://blog.csdn.net/Firefly_cjd/article/details/79825869 以太网PHY寄存器分析 1 1.以太网PHY标准寄存器分析 2 ...
- 20187101021-王方《面面相对象程序设计java》第十三周实验总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...