python多线程实践小结
参考:http://www.cnblogs.com/tqsummer/archive/2011/01/25/1944771.html
#!/usr/bin/env python
import sys
import threading import serial
#from threading import Thread
from time import sleep sub_msg_lock = threading.Lock() class thread1_test(threading.Thread): def __init__(self,my_thread_name1,para_for_thread1):
threading.Thread.__init__(self,name = my_thread_name1)
self.thread1_num = para_for_thread1
#self.thread1_stop_flag = False def run(self):#name must is:run
thread1_count =
while thread1_count < :#not self.thread1_stop_flag
print("thread1 started")
sleep(0.5)
self.thread1_num = self.thread1_num +
print("thread1_num:",self.thread1_num)
thread1_count += #def thread1_stop(self):
#self.thread1_stop_flag = True class thread2_test(threading.Thread): def __init__(self,my_thread_name2,para_for_thread2):
threading.Thread.__init__(self,name = my_thread_name2)
self.thread2_num = para_for_thread2
#self.thread2_stop_flag = False def run(self):
thread2_count =
while thread2_count < :#not self.thread1_stop_flag
print("thread2 started")
sleep()
self.thread2_num = self.thread2_num +
print("thread2_num:",self.thread2_num)
thread2_count += def thread2_stop(self):
#thread2_test_instance = thread2_test()
#thread2_name = thread2_test_instance.getName()
#wrong: getName() is method of thread,so must used by instance of thread
#self.thread2_stop_flag = True if __name__ == '__main__':#this is the main thread
thread1 = thread1_test('wang_thread1',)
thread2 = thread2_test('wang_thread2',) thread1.setDaemon(True)
thread2.setDaemon(True)
thread1.start()
thread2.start()
sleep() thread1_name = thread1.getName()
print("thread1_name:",thread1_name)
thread2_name = thread2.getName()
print("thread2_name:",thread2_name) thread1_isdaemon = thread1.isDaemon()
#default Flase: means the son thread will not died with main thread (son has freedom)
#we can through setDaemon(True) to make son threads die with main thread (son can not longer than main) = (main died son must died,son can ealyer died than mian)
print("thread1_isdaemon:",thread1_isdaemon)
thread2_isdaemon = thread2.isDaemon()
print("thread2_isdaemon:",thread2_isdaemon) #thread1.thread1_stop()
#thread2.thread2_stop() thread1_isalive = thread1.isAlive()
print("thread1_isalive:",thread1_isalive)
thread2_isalive = thread2.isAlive()
print("thread2_isalive:",thread2_isalive) #thread1.join()
thread2.join()#main thread wait thread1 and thread2 then main thread go on
#sleep()
print ("mian thread terminate!")
print ("All threads terminate!") 关于线程的方法:
就我个人而言,比较喜欢第二种方式,即创建自己的线程类,必要时重写threading.Thread类的方法,线程的控制可以由自己定制。
threading.Thread类的使用:
1,在自己的线程类的__init__里调用threading.Thread.__init__(self, name = 'threadname') 其中threadname为线程的名字
2, run(),通常需要重写,编写代码实现做需要的功能。
3,getName(),获得线程对象名称
4,setName(),设置线程对象名称
5,start(),启动线程
6,jion([timeout]),等待另一线程结束后再运行。不设置timeout则等待该线程结束,设置timeout后,最多等待线程timeout这么长时间,然后继续运行下面的程序.
7,setDaemon(bool),设置子线程是否随主线程一起结束,必须在start()之前调用。默认为False,即子线程自由与主线程.否则,主线程结束时不管子线程是否结束,都会结束子线程.
8,isDaemon(),判断线程是否随主线程一起结束。
9,isAlive(),检查线程是否在运行中。
此外threading模块本身也提供了很多方法和其他的类,可以帮助我们更好的使用和管理线程。可以参看http://www.python.org/doc/2.5.2/lib/module-threading.html。
python多线程实践小结的更多相关文章
- 多线程实践—Python多线程编程
多线程实践 前面的一些文章和脚本都是只能做学习多线程的原理使用,实际上什么有用的事情也没有做.接下来进行多线程的实践,看一看在实际项目中是怎么使用多线程的. 图书排名示例 Bookrank.py: 该 ...
- 基于Windows平台的Python多线程及多进程学习小结
python多线程及多进程对于不同平台有不同的工具(platform-specific tools),如os.fork仅在Unix上可用,而windows不可用,该文仅针对windows平台可用的工具 ...
- Python 多线程教程:并发与并行
转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...
- 浅析Python多线程
学习Python多线程的资料很多,吐槽Python多线程的博客也不少.本文主要介绍Python多线程实际应用,且假设读者已经了解多线程的基本概念.如果读者对进程线程概念不甚了解,可参见知名博主 阮一峰 ...
- Python 多线程、多进程 (二)之 多线程、同步、通信
Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.python ...
- Python多线程问题的资料查找与汇总
Python多线程问题的资料查找与汇总 声明: 1)本报告由博客园bitpeach撰写,版权所有,免费转载,请注明出处,并请勿作商业用途. 2)若本文档内有侵权文字或图片等内容,请联系作者bitpea ...
- Python多线程原理与实现
Date: 2019-06-04 Author: Sun Python多线程原理与实战 目的: (1)了解python线程执行原理 (2)掌握多线程编程与线程同步 (3)了解线程池的使用 1 线程基本 ...
- python多线程与多进程及其区别
个人一直觉得对学习任何知识而言,概念是相当重要的.掌握了概念和原理,细节可以留给实践去推敲.掌握的关键在于理解,通过具体的实例和实际操作来感性的体会概念和原理可以起到很好的效果.本文通过一些具体的例子 ...
- Python机器学习实践指南pdf (中文版带书签)、原书代码、数据集
Python机器学习实践指南 目 录 第1章Python机器学习的生态系统 1 1.1 数据科学/机器学习的工作 流程 2 1.1.1 获取 2 1.1.2 检查和探索 2 1.1.3 清理和准备 3 ...
随机推荐
- null 理解
值 null 特指对象的值未设置.它是 JavaScript 基本类型 之一. 语法节 null 描述节 值 null 是一个字面量,它不像undefined 是全局对象的一个属性.null 是表示缺 ...
- VIM C语言函数名高亮
VIM默认情况下,函数名是不会高亮的,将下面这段代码添加到/usr/share/vim/vim73/syntax/c.vim文件的末尾即可: "highlight Functions s ...
- Windows 10 Mac 为Vs Code配置C/C++环境
2019-06-10 更新: 加上Mac版本的Vscode配置文件 0.前言 实现效果:右键一键编译运行C/C++文件 Vs code的代码效果很好看,也很轻量,所以想为Vs Code配置C/C++环 ...
- docker build no such file or directory
在我构建新的镜像的时候, 发生 了 no such file or directory 的错误. 这个错误找了半天, 没头绪, 后来灵光一现, 原来是我的文件夹名字写错了 我的目录结构是这样的 [ ...
- JS获取单选框checked的value方法
; var obj = document.getElementsByTagName("input"); document.getElementById('gender').oncl ...
- $monitor用法
1.$monitor 进程同一时间有且仅有一个,若多次调用$monitor,新进程会代替以前的monitor进程. 2.$fmonitor可以同时存在任意个. 3.一般不用$monitor系统函数. ...
- 【php】 给数组重建索引
array_values $a = [1,2]; unset($a[1]); $a[] = 3; print_r($a); // 输出 [0=>1,2=>3]
- PAT Basic 1052
1052 卖个萌 萌萌哒表情符号通常由“手”.“眼”.“口”三个主要部分组成.简单起见,我们假设一个表情符号是按下列格式输出的: [左手]([左眼][口][右眼])[右手] 现给出可选用的符号集合,请 ...
- 在html页面中使用js变量
Method 1: <a id="xxxx">xxxxxxxxxxxxxxxxxx</a> <script type="text/jav ...
- thymeleaf和artTemplate
Company最近项目中使用了两个模板引擎,分别是Java服务器端的模板引擎Thymeleaf和前端的模板引擎artTemplate, 其实对于这两个模板引擎 理论上应该是不应该放在一起记录的,但是b ...