参考: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. 分布式mysql 和 zk ( zookeeper )的分布式的区别 含冷热数据讨论

    zk ( zookeeper )的分布式仅仅指的是备份模式. 分布式 mysql 不仅仅要关注备份(从以往的半主,主主,到 paxos). (mysql 比 hbase 的region成熟, hdfs ...

  2. LeetCode 三角形最小路径和

    给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径和为 11 ...

  3. modelsim安装调试

    modelsim,debug:“unable to checkout a viewer license necessary for use of the modelsim graphical user ...

  4. Python旅途——入门基础

    1.入门 ​ 作为近几年计算机程序设计语言中很火的Python,是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大 ...

  5. logging日志模块,四种方式

    1.最简单的用法 import logging logging.error("hah") logging.info("hah") logging.debug(& ...

  6. foxmial 和 outlook设置问题

    您可以使用支持POP3的客户端软件(例如Foxmail或Outlook)收发您的邮件.请配置您的电子邮件客户端,以下载QQ邮箱邮件. 了解如何进行配置,请单击您的电子邮件客户端名称: Foxmail设 ...

  7. 杭电 1503 Advanced Fruits

    Description The company "21st Century Fruits" has specialized in creating new sorts of fru ...

  8. POJ 2486 树形背包DP Apple Tree

    设d(u, j, 0)表示在以u为根的子树中至多走k步并且最终返回u,能吃到的最多的苹果. 则有状态转移方程: #include <iostream> #include <cstdi ...

  9. linux中软件包管理

    一.流行的软件包管理有两种: Debian Linux的Deb软件包和 Redhat Linux的Rpm软件包, Debian Linux首先提出将应用程序的二进制文件.配置文档.man/info帮助 ...

  10. 如何反馈问题issue?

    如何反馈问题issue? 01,请提交的时候换位思考一下:如果别人给你提交一个这样的 Issue,你能快速准确的理解吗?如果不能,烦请重新整理你的语言,按照要求的格式填写.专业一点,减少不必要的口舌浪 ...