Python多线程学习
来源:互联网 发布:英雄联盟网络连接失败 编辑:程序博客网 时间:2024/06/04 17:41
一.创建线程
1.通过thread模块中的start_new_thread(func,args)创建线程:
在Eclipse+pydev中敲出以下代码:
1.通过thread模块中的start_new_thread(func,args)创建线程:
在Eclipse+pydev中敲出以下代码:
# -*- coding: utf-8 -*- import thread def run_thread(n): for i in range(n): print i thread.start_new_thread(run_thread,(4,)) #参数一定是元组,两个参数可以写成(a,b)
运行报错如下:
Unhandled exception in thread started by sys.excepthook is missinglost sys.stderr
网上查出原因是不建议使用thread,然后我在pythonGUI中做了测试,测试结果如下,显然python是支持thread创建多线程的,在pydev中出错原因暂时不明。
>>> import thread>>> def run(n):for i in range(n):print i>>> thread.start_new_thread(run,(4,))985201>>> 23
2.通过继承threading.Thread创建线程,以下示例创建了两个线程
# -*- coding: utf-8 -*- '''Created on 2012-8-8@author: jeromewei ''' from threading import Threadimport timeclass race(Thread): def __init__(self,threadname,interval): Thread.__init__(self,name=threadname) self.interval = interval self.isrunning = True def run(self): #重写threading.Thread中的run() while self.isrunning: print 'thread %s is running,time:%s\n' %(self.getName(),time.ctime()) #获得线程的名称和当前时间 time.sleep(self.interval) def stop(self): self.isrunning = Falsedef test(): thread1 = race('A',1) thread2 = race('B',2) thread1.start() thread2.start() time.sleep(5) thread1.stop() thread2.stop() if __name__ =='__main__': test()
3. 在threading.Thread中指定目标函数作为线程处理函数
# -*- coding: utf-8 -*- from threading import Thread def run_thread(n): for i in range(n): print i t1 = Thread(target=run_thread,args=(5,))#指定目标函数,传入参数,这里参数也是元组t1.start() #启动线程
二. threading.Thread中常用函数说明
函数名 功能run()如果采用方法2创建线程就需要重写该方法getName()获得线程的名称(方法2中有示例)setName()设置线程的名称start()启动线程join(timeout) 在join()位置等待另一线程结束后再继续运行join()后的操作,timeout是可选项,表示最大等待时间setDaemon(bool) True:当父线程结束时,子线程立即结束;False:父线程等待子线程结束后才结束。默认为FalseisDaemon()判断子线程是否和父线程一起结束,即setDaemon()设置的值isAlive() 判断线程是否在运行
以上方法中,我将对join()和setDaemon(bool)作着重介绍,示例如下:
(1)join方法:
# -*- coding: utf-8 -*- import threading import time #导入time模块 class Mythread(threading.Thread): def __init__(self,threadname): threading.Thread.__init__(self,name = threadname) def run(self): time.sleep(2) for i in range(5): print '%s is running····'%self.getName() t2 = Mythread('B') t2.start()#t2.join() for i in range(5): print 'the program is running···'
这时的程序流程是:主线程先运行完,然后等待B线程运行,所以输出结果为:
the program is running···the program is running···the program is running···B is running····B is running····B is running····
如果启用t2.join(),这时程序的运行流程是:当主线程运行到t2.join()时,它将等待t2运行完,然后再继续运行t2.join()后的操作,呵呵,你懂了吗,所以输出结果为:
B is running····B is running····B is running····the program is running···the program is running···the program is running···
(2)setDaemon方法:
# -*- coding: utf-8 -*- import threadingimport time class myThread(threading.Thread): def __init__(self, threadname): threading.Thread.__init__(self, name=threadname) def run(self): time.sleep(5) print '%s is running·······done'%self.getName() t=myThread('son thread')#t.setDaemon(True)t.start()if t.isDaemon(): print "the father thread and the son thread are done"else: print "the father thread is waiting the son thread····"
这段代码的运行流程是:主线程打印完最后一句话后,等待son thread 运行完,然后程序才结束,所以输出结果为:
the father thread is waitting the son thread····son thread is running·······done
如果启用t.setDaemon(True),这段代码的运行流程是:当主线程打印完最后一句话后,不管son thread是否运行完,程序立即结束,所以输出结果为:
the father thread and the son thread are done
三. 小结
介绍到这里,python多线程使用级别的知识点已全部介绍完了,下面我会分析一下python多线程的同步问题。
0 0
- Python多线程学习
- python 多线程学习
- Python多线程学习
- Python多线程学习
- Python多线程学习
- Python多线程学习
- Python多线程学习
- Python多线程学习
- Python多线程学习
- Python多线程学习
- python学习_多线程
- python多线程学习
- Python多线程学习
- python 多线程学习一
- Python多线程学习
- Python多线程学习
- Python多线程学习
- Python多线程学习<转>
- mac os 升级到10.11后,Xcode问题
- ActiveX控件开发小结
- 认识JS
- 最简实例说明wait、notify、notifyAll的使用方法
- USACO 香甜的黄油
- Python多线程学习
- C++标准库头文件
- javascript条件语句
- 关于STC15F2K60S2看门狗的应用
- js快速判断IE浏览器(兼容IE10与IE11)
- Ubuntu下关于crontab的使用详解
- STC15F2K60S2内E2PROM应用
- SQL语言的应用
- Spark开发环境之windows配置