参考: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多线程实践小结的更多相关文章

  1. 多线程实践—Python多线程编程

    多线程实践 前面的一些文章和脚本都是只能做学习多线程的原理使用,实际上什么有用的事情也没有做.接下来进行多线程的实践,看一看在实际项目中是怎么使用多线程的. 图书排名示例 Bookrank.py: 该 ...

  2. 基于Windows平台的Python多线程及多进程学习小结

    python多线程及多进程对于不同平台有不同的工具(platform-specific tools),如os.fork仅在Unix上可用,而windows不可用,该文仅针对windows平台可用的工具 ...

  3. Python 多线程教程:并发与并行

    转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...

  4. 浅析Python多线程

    学习Python多线程的资料很多,吐槽Python多线程的博客也不少.本文主要介绍Python多线程实际应用,且假设读者已经了解多线程的基本概念.如果读者对进程线程概念不甚了解,可参见知名博主 阮一峰 ...

  5. Python 多线程、多进程 (二)之 多线程、同步、通信

    Python 多线程.多进程 (一)之 源码执行流程.GIL Python 多线程.多进程 (二)之 多线程.同步.通信 Python 多线程.多进程 (三)之 线程进程对比.多线程 一.python ...

  6. Python多线程问题的资料查找与汇总

    Python多线程问题的资料查找与汇总 声明: 1)本报告由博客园bitpeach撰写,版权所有,免费转载,请注明出处,并请勿作商业用途. 2)若本文档内有侵权文字或图片等内容,请联系作者bitpea ...

  7. Python多线程原理与实现

    Date: 2019-06-04 Author: Sun Python多线程原理与实战 目的: (1)了解python线程执行原理 (2)掌握多线程编程与线程同步 (3)了解线程池的使用 1 线程基本 ...

  8. python多线程与多进程及其区别

    个人一直觉得对学习任何知识而言,概念是相当重要的.掌握了概念和原理,细节可以留给实践去推敲.掌握的关键在于理解,通过具体的实例和实际操作来感性的体会概念和原理可以起到很好的效果.本文通过一些具体的例子 ...

  9. Python机器学习实践指南pdf (中文版带书签)、原书代码、数据集

    Python机器学习实践指南 目 录 第1章Python机器学习的生态系统 1 1.1 数据科学/机器学习的工作 流程 2 1.1.1 获取 2 1.1.2 检查和探索 2 1.1.3 清理和准备 3 ...

随机推荐

  1. Linux网卡设置为网桥模式

    Linux网卡设置为网桥模式 1.    添加网卡,并修改相关配置文件 1.1虚拟机添加网卡,并配置相关文件 如:eth2为新添加网卡 cd /etc/sysconfig/network-script ...

  2. CS193p Lecture 10 - Multithreating, UIScrollView

    Multithreating(多线程) 网络请求例子: NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithStrin ...

  3. 三:MySql数据库及连接

    前言: 开发中团队使用一个MYSQL数据库,我们只需要知道怎么去连接这个已经存在的数据库即可,因此关于MYSQL数据库安装部分可以去Baidu,并不是主要关心的部分 学会在windows7下使用DOS ...

  4. Linux中文件压缩与解压

    压缩与解压 compress 文件名 1 -v //详细信息 2 3 -d //等于 uncompress 默认只识别 .Z 如果使用别的后缀,会导致不识别,解压缩失败.也可以使用 -d -c 压缩包 ...

  5. 纯 CSS 创作一个小球绕着圆环盘旋的动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gKxyWo 可交互视频 ...

  6. AHB2reg接口转换

    assign mcu_xxx_addr = (rd_after_wr_reg || reg_valid_write_trans) ? haddr_reg[ADDR_WIDTH+:] : haddr[A ...

  7. django第三天(路由基础和路由分配)

    路由基础 url(正则路径,视图函数地址,默认关键字参数,路由别名) 路由由上而下匹配, ""可以匹配任意路由 "^$"来匹配"/" url ...

  8. Day05基本运算符,if判断和while循环

    day05 1.常量 变量名全大写 2.基本运算符 ①算术运算 10/3除法 10//3取整 10*3乘法 10**3幂 ②赋值运算 增量赋值 age += 1#age = age + 1 age * ...

  9. Python9-面对对象2-day23

    #计算正方形的周长和面积 class Square: def __init__(self,side_len): self.side_len = side_len def perimeter(self) ...

  10. python基础——12(包的概念)

    一.模块 1.模块的加载顺序 加载顺序:内存-->内置-->sys.path(一系列自定义模块) import sys sys.path  #环境变量:存放文件路径的列表 重点:默认列表的 ...