Python多线程学习
一.创建线程
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 missing
lost 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,))
98520
1
>>>
2
3
2.通过继承threading.Thread创建线程,以下示例创建了两个线程
# -*- coding: utf-8 -*-
'''
Created on 2012-8-8
@author: jeromewei
'''
from threading import Thread
import time
class 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 = False
def 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:父线程等待子线程结束后才结束。默认为False |
isDaemon() |
判断子线程是否和父线程一起结束,即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 threading
import 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多线程的同步问题。
分享到:
相关推荐
Python多线程学习教程
threading模块和thread模块练习
这是python多线程学习的一些资料,希望能帮助大家!
这个是在学习python多线程的时候自己总结的文档,对理解Python多线程非常有帮助,很多都是从官方文档而来的。
python多线程编程,适合进阶型学习者,实现多线程下的编程。
程序运行起来,叫进程,进程是资源分配的单位,线程执行代码,一个线程只能执行一个任务,想要执行多个任务,就需要多线程 协程依赖于线程,线程依赖于进程,协程切换需要资源相当少,所以效率就会很高 重构:把...
pythonthread(Python多线程处理).docx
Python多线程编程.zipPython多线程编程.zipPython多线程编程.zipPython多线程编程.zipPython多线程编程.zipPython多线程编程.zipPython多线程编程.zipPython多线程编程.zipPython多线程编程.zip 运行python的时候,...
Python3的多线程学习笔记[定义].pdf
多线程学习,通过两个函数来测试多线程的运行原理,运行时可以反复看时间的先后
这篇文章主要介绍了python多线程并发及测试框架案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1、循环创建多个线程,并通过循环启动执行 import ...
一、Python中的线程使用: Python中使用线程有两种方式:函数或者用类来包装线程对象。 1、 函数式:调用thread模块中的start_new_thread()函数来产生新线程。如下例: 复制代码 代码如下: import time import ...
主要介绍了Python多线程下的list的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
PyQt5中异步刷新UI和Python中的多线程总结 学习python和PyQt5的过程中,做了demo,可以从电脑端向手push文件和安装apk的GUIdemo,初学者可从这个例子中学到很多知识。涉及到PyQt5中异步刷新UI+Python中的多线程+...
当然是多线程的优势已经掩盖了它本身不足之处,所以我们来加强一下学习python多线程的信心吧~ 总结起来,使用多线程编程具有如下几个优点: 进程之间不能共享内存,但线程之间共享内存非常容易。 操作系统在创建进程...
python基础_31_Python_threading_1_什么是多线程_(教学教程tutorial)
批量爬虫下载时,单线程下载文件有时慢有时快。...同时附上单线程下载(带进度条显示功能)代码,单线程下载(带进度条显示)和多线程代码的时间比较,实测多线程能有效提升下载效率,效果还是比较不错的。
python学习笔记; 内容包括:信号与槽; 多线程 类的学习