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 ...
随机推荐
- request :fail url not in domain list
1.可点击开发者工具右上角 详情-域名信息,看看是否配置了域名: 2. 手机预览小程序的时候,打开调试
- Bootstrap历练实例:警告样式按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- shell脚本,每5个字符之间插入"|",行末不插入“|”。
文本aaaaabbbbbcccccdddd eeeeefffffkkkkkvvvv nnnnnggggg 希望得到的结果如下: aaaaa|bbbbb|ccccc|dddd eeeee|fffff|k ...
- java第九次作业:第九章例题3个
作业1: 例题9.1 制作圆类,根据圆的半径求出周长及面积 package com.swift; //抽象的方法构成类,把属性和方法进行封装 public class Circle { // 两个方面 ...
- iOS dateformatter设置GMT格式时间--iOS开发系列---项目中成长的知识四
今天在项目中开始接手客户端的签名这个模块,签名这个会在项目结束过后再单独写一下自己的心得! 今天讲讲在签名的过程中我们需要向服务器传送一个Date值,格式要求是格林威治时间,也就是GMT时间! 格式要 ...
- 将Xcode的本地代码push到github仓库上
1.首先,你得有一个github账号,如果没有的话就去注册一个,通过下面图片的方式创建一个github仓库. 2.创建仓库后填写相关的信息,比如说仓库名等. 3.在xcode上进行设置,添加远程git ...
- 【模板】任意模数NTT
题目描述: luogu 题解: 用$fft$水过(什么$ntt$我不知道). 众所周知,$fft$精度低,$ntt$处理范围小. 所以就有了任意模数ntt神奇$fft$! 意思是这样的.比如我要算$F ...
- PHP 把字符转换为 HTML 实体 - htmlentities() 函数
定义和用法 htmlentities() 函数把字符转换为 HTML 实体. 语法 htmlentities(string,quotestyle,character-set) 参数 描述 string ...
- 【Java_基础】java中的多态性
方法的重载.重写和动态链接构成了java的多态性. 1.方法的重载 同一个类中多个同名但形参有所差异的方法,在调用时会根据参数的不同做出选择. 2.方法的重写 子类中重新定义了父类的方法,有关方法重写 ...
- [php] 表单注意事项
什么是 htmlspecialchars()方法? htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体. 预定义的字符是: & (和号) 成为 & &q ...