【python标准库学习】thread,threading(一)多线程的介绍和使用

在单个程序中我们经常用多线程来处理不同的工作,尤其是有的工作需要等,那么我们会新建一个线程去等然后执行某些操作,当做完事后线程退出被回收。当一个程序运行时,就会有一个进程被系统所创建,同时也会有一个线程运行,这个线程就是主线程main,在主线程中所创建的新的线程都是子线程,子线程通常都是做一些辅助的事。python中提供了thread和threading两个模块来支持多线程。这篇介绍一下python的多线程和基本使用,下一篇介绍python多线程同步问题。

        python中使用线程有两种方式,第一种是用thread模块的start_new_thread函数,另一种是用threading模块的Thread类来包装线程对象。

1.使用thread模块

        使用thread模块的start_new_thread函数创建线程并启动。start_new_thread函数原型:

        thread.start_new_thread(function, args[, kwargs])

>>> help(thread.start_new_thread)

 

Help on built-in function start_new_thread in module thread:

 

start_new_thread(...)

    start_new_thread(function, args[, kwargs])

    (start_new() is an obsolete synonym)

    

    Start a new thread and return its identifier.  The thread will call the

    function with positional arguments from the tuple args and keyword arguments

    taken from the optional dictionary kwargs.  The thread exits when the

    function returns; the return value is ignored.  The thread will also exit

    when the function raises an unhandled exception; a stack trace will be

    printed unless the exception is SystemExit.

(END) 

  

这个函数创建一个新线程,并立刻执行函数function,args是该函数function的参数,是一个元组,kwargs是可选的,它为函数提供了命名参数字典。该函数返回一个整数,为线程标示符。function函数执行完后线程正常退出,如果执行过程中有未处理的异常,线程会异常退出。

        thread模块的主要函数:

        1).start_new_thread()

           创建并启动线程执行function函数,function执行完线程退出,或者遇到未处理的异常线程异常退出。

        2).thread.exit()

           结束当前线程,会触发SystemExit异常,如果没有捕获处理该异常,线程会退出。

        3).thread.get_ident()

           得到该线程的标示符,就是新建线程时返回的标示符,是一个整数。

        4).thread.interrupt_main

           在主线程main中触发一个KeyboardInterrupt异常,子线程用这个函数来终止主线程。

        5).thread.allocate_lock()

           创建一个锁对象LockType,使多个线程同步访问共享资源。

        在python中使用多线程更多的是使用第二种方式,即使用threading模块。

2.使用threading模块

        threading模块是对thread模块的第二次封装,提供了更好的API来让我们使用。使用threading实现多线程编程都是使用的Thread类,或者Timer类,Timer是Thread的子类,可以指定时间间隔后在执行某个操作:

 1 from threading import Timer
 2 
 3  
 4 
 5 def main():
 6 
 7     print 'timer test!'
 8 
 9 t = Timer(3, main)
10 
11 t.start()

使用Thread类有两种方式,一种是直接创建Thread类的实例,另一种方式是自定义类来继承Thread类。使用Thread类的实例,在它的初始化函数__init__中将可调用的对象作为参数传入,Thread的初始化函数__init__原型:

        __init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None);

group和verbose不知道是什么东西,target是可调用对象,线程启动后就执行它,name为线程的名字,默认为"Thread-N",N为数字,args和kwargs是调用target是的参数列表和关键字参数。

1 from threading import Thread
2 
3 def main(a, b):
4 
5     print 'a:%d, b:%d' % (a, b)
6 
7 Thread(target=main, name='newThread', args=(1, 2)).start()

自定义类来继承Thread类,然后重写run方法实现我们自己的操作。

from threading import Thread

 

 

class MyThread(Thread):

 

    def __init__(self, threadName, a, b):

        Thread.__init__(self, name=threadName)

        self.a = a

        self.b = b

 

    def run(self):

        print 'a:%d, b:%d' % (self.a, self.b)

 

 

myThread = MyThread('newThread', 1, 2)

myThread.start()

逻辑代码是写在run方法中的,但是我们启动线程都是用的start方法,这两个方法是有区别的,如果直接调用run()方法,只是单纯的调用方法,而调用start()方法是将线程从新建状态启动编程就绪状态,等待cpu的调度,这里涉及到线程的几种状态,start()后线程没有立即运行而是编程就绪状态,当得到cpu时间片后编程运行状态,当run方法结束或者有未处理的异常,线程就结束变成死亡状态等待被回收,在运行状态如果遇到资源没有准备好就会变成阻塞状态,当得到资源后再次编程就绪状态等待cpu的调度。

        threading模块的主要类:

        1).Thread类

           上面已经介绍。

        2).Timer类

           Thread的子类,上面已经介绍。

        3).Lock,RLock,Condition类

           同步锁,用于多实现线程同步,资源共享的问题。

        4).Event类

           用于多线程通信。

        多线程同步锁Lock,RLock,Condition和多线程通信Event后面在多线程同步再介绍。

        threading模块Thread类的主要函数:

        1).Thread.getName(),Thread.setName()

           获取和设置线程名字。

        2).Thread.ident()

           获取线程的标示符,是一个整数。

        3).Thread.is_alive(),Thread.isAlive()

           判断线程是不是活的,即线程是否已经结束。

        4).Thread.activeCount()

           获取当前活着的所有线程总数,包括主线程main。

        5).Thread.join()

           使主线程阻塞,知道该子线程执行完或者超时,它有一个参数timeout表示超时时间,默认为None。

        6).Thread.setDaemon()

           有一个布尔值的参数,默认为False,该方法设置子线程是否随主线程一起结束。

        看看join方法和setDaemon方法:

import threading, time

 

 

class MyThread(threading.Thread):

 

    def __init__(self):

        threading.Thread.__init__(self)

 

    def run(self):

        print 'thread start'

        time.sleep(3)

        print 'thread end'

 

 

thread1 = MyThread()

thread1.start()

 

time.sleep(1)

 

print 'start join'

#thread1.join()

print 'end join'

  输出结果:

不加thread.join

thread start

start join

end join

thread end

 

加thread.join

thread start

start join

thread end

end join

  主线程中sleep(1)一秒就是为了让线程被调度,线程中sleep(3)三秒就是为了让主线程结束,从两种情况的输出结果可以看出join的功能。

import threading, time

 

 

class MyThread(threading.Thread):

 

    def __init__(self):

        threading.Thread.__init__(self)

 

    def run(self):

        print 'thread start'

        time.sleep(3)

        print 'thread end'

 

 

print 'main start'

thread1 = MyThread()

#thread1.setDaemon(True)

thread1.start()

 

time.sleep(1)

 

print 'main end'

  输出结果:

setDaemon默认为False

main start

thread start

main end

thread end

 

setDaemon设置为True

main start

thread start

main end

  

从打印的结果可以看出,当设置为True的时候,线程还在sleep过程就就结束了,以至于thread end这句都没有打印出来。

        当然Thread类还不止这些方法,这些只是常用的方法,通过help(Thread)可以查看。

posted @ 2018-07-30 19:48  o微凉o  阅读(406)  评论(0编辑  收藏  举报