【Python之旅】第七篇(一):再谈P

发布时间:2019-06-26 09:48:38编辑:auto阅读(1341)

        主要是再进一步加深Python中关于多线程相关函数join()的理解以解多线程的执行过程。这里通过下面的例子来作进一步的说明。


    1.多线程与主程序代码的执行顺序关系

        给出下面程序代码:

    #!/usr/bin/env python
    
    import threading
    import time
    
    def sayHi(n):
    	time.sleep(1)
    	print 'Hi this is thread %s' %n
    
    thread_list = []    #用来存放多线程执行返回的函数入口,因此这里存放的是函数入口
    
    for i in range(20):
    	thread_list.append(threading.Thread(target=sayHi, args=(i,)))
    
    for i in thread_list:    #执行列表中存放的函数入口,只有执行了之后才会执行sayHi()的程序代码,否则上面只是把函数入口存放在这个列表中而已
    	i.start()
    
    for i in thread_list:    #检测对应的线程是否已经执行完毕(即函数入口是否已经被执行,从而执行相关的程序代码)
    	i.join()    #join()中可以加超时时间,如i.join(3),表示当前i对应的函数入口所执行的
    	线程,如果在3秒内还没有执行完就超时,在线程可以正常执行的时候不加也是没有关系的,
    	但是当线程出现异常而无法正常执行时,由于i.join()还没有检测到线程已经执行完毕,所
    	以会一直处于等待状态,这样的话就会造成程序的代码不能继续执行(程序还停留在i.join(
    	)这里等待这一个其对应的线程执行完毕),设定超时时间就可以避免这一个问题,下面会有相关说明
    
    print '\033[32;1mThis is the last line\033[0m'

        程序执行结果如下:

    xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7$ python day6thread1.py 
    Hi this is thread 0
    Hi this is thread 1
    Hi this is thread 2
    Hi this is thread 3
    Hi this is thread 4
    Hi this is thread 5
    Hi this is thread 6
    Hi this is thread 7
    Hi this is thread 8
    Hi this is thread 9
     Hi this is thread 11
     Hi this is thread 14
     Hi this is thread 17
     Hi this is thread 12
    Hi this is thread 15
    Hi this is thread 16
    Hi this is thread 10
    Hi this is thread 13
    Hi this is thread 18
    Hi this is thread 19
    This is the last line

        将程序代码修改为如下:

    #!/usr/bin/env python
    
    import threading
    import time
    
    def sayHi(n):
    	time.sleep(1)
    	print 'Hi this is thread %s' %n
    
    thread_list = []
    
    for i in range(20):
    	thread_list.append(threading.Thread(target=sayHi, args=(i,)))
    
    for i in thread_list:
    	i.start()
    
    #for i in thread_list:
    #	i.join()
    
    print '\033[32;1mThis is the last line\033[0m'

        程序执行结果如下:

    xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7$ python day6thread1.py 
    This is the last line
    Hi this is thread 0
     Hi this is thread 2
     Hi this is thread 6
     Hi this is thread 9
     Hi this is thread 10
     Hi this is thread 15
     Hi this is thread 16
     Hi this is thread 3
    Hi this is thread 8
    Hi this is thread 11
     Hi this is thread 14
    Hi this is thread 17
    Hi this is thread 5
    Hi this is thread 13
    Hi this is thread 4
    Hi this is thread 1
    Hi this is thread 7
    Hi this is thread 12
    Hi this is thread 18
    Hi this is thread 19

        对比两个例子可以发现,不同之处在于“This is the last line”的输出位置,未修改代码前是在最后,而修改代码后则在最前,作如下解释:

    第一个例子由于加了join()作检测,程序的代码会停在i.join()的代码块中,而这里又没有设置超时时间,因此会直到检测到所有的进程都执行完毕才开始执行该代码块后面的程序代码,因此,“This is the last line”会在最后面输出;

    第二个例子没有加join()作检测,所以不管线程是否已经执行完毕,只要把所有函数入口加入线程中开始执行,就马上执行i.start()代码块后面的程序代码,由于多线程执行的函数加了sleep(1),所以线程执行的输出肯定比后面打印“This is the last line”要慢,因此,这一句会在最前面输出。

        多线程是这样,多进程也是类似的,前面已经有详细的例子和说明。


    2.有关于join()的进一步解释说明

        其实在第一个例子的程序代码中已经给出了join()的解释说明,这里只需要再看下面一个例子就更加好理解了。

        程序代码如下:

    #!/usr/bin/env python
    
    import threading
    import time
    
    def sayHi(n):
    	time.sleep(2)
    	print 'Hi this is thread %s' %n
    
    thread_list = []
    
    for i in range(10):
    	thread_list.append(threading.Thread(target=sayHi, args=(i,)))
    
    for i in thread_list:
    	i.start()
    
    for i in thread_list:
    	print i.join(0.1)
    
    print '\033[32;1mThis is the last line\033[0m'

        程序执行结果如下:

    xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7$ time python day6thread1.py 
    None
    None
    None
    None
    None
    None
    None
    None
    None
    None
    This is the last line
    Hi this is thread 0
    Hi this is thread 1
    Hi this is thread 2
    Hi this is thread 3
    Hi this is thread 4
    Hi this is thread 5
     Hi this is thread 7
     Hi this is thread 8
    Hi this is thread 6
    Hi this is thread 9
    
    real	0m2.080s
    user	0m0.048s
    sys	0m0.016s

        作如下解释说明:

    1)程序代码到了i.join()时,由于0.1秒超时,而线程执行函数sleep2秒;

    2)0.1秒过去后,第一个线程超时,i.join()检测第二个线程,打印输出None;

    3)0.1秒过去后,第二个线程超时,i.join()检测第三个线程,打印输出None;

    ……

    4)检测完10个线程,用了1秒,此时join()的工作完成(总共输出了10个None),执行join()后面的代码块;

    5)join()后面的代码块输出“This is the last line”;

    6)再过1秒后,所有线程sleep完,总共输出10句“Hi this is thread ”;

    7)程序执行完毕,进程结束。

        通过上面这一个例子的分析,相信无论是对多线程的执行过程,还是对join()函数的理解,都会有更进一步的认识。


关键字