笔记—线程threading.Thread基础

线程(threading):

        当我们执行一个主线程时,可以产生一(多)个分线程,这样可以兵分两(多)路处理任务

        线程实现方法主要有两种:一种是定义函数式,大家都推荐这种方法(还有一种是使用类的方法,要求能看懂)

        线程中必须掌握几个基础方法:t.start(), t.join(), t.setDaemon().

例子1:t.start()方法

            同时碰到两个人,简单的sayHello函数以及回应函数:

import threading
import time
def sayHello(args):
    print('Hello',args,time.ctime())
    time.sleep(3)
    print('Nice to meet you, Li')

if __name__ == '__main__':
    t1 = threading.Thread(target = sayHello,args = ('John',))
    t1.start()

    t2 = threading.Thread(target = sayHello,args = ('Kim',))
    t2.start()

    print('end... ... ')

提醒:① 注意最后输出 Process finished with exit code 0 的时间,因为time.sleep(3), 主线程是默认会等待分线程执行结束后才停止的。

          ② 其实线程1和线程2开始的时候,主线程并不会受影响,而是继续往下执行输出“end... ...”但是主线程运行完成后并不会马上退出而是等待分线程结束后才停止。

下面我们看一下join()对线程运行的影响:

例子2(1):t.join()方法

    

import threading
import time

def playGame(gameName):
    print("begin to play %s at %s" % (gameName,time.ctime()))
    time.sleep(3)
    print("stop to  play %s at %s" % (gameName,time.ctime()))

def watchVideo(videoName):
    print("begin to watch videoName %s at %s" % (videoName,time.ctime()))
    time.sleep(5)
    print("stop to watch videoName %s at %s" % (videoName,time.ctime()))

if __name__ == '__main__':

    t1=threading.Thread(target=playGame,args = ('Wilderness hunter ',))
    t2=threading.Thread(target=watchVideo,args = ('Tokyo hot',))

    t1.start()
    t2.start()

    t1.join()
    t2.join()

    print("ending")
输出:

提醒:① 注意输出时间的变化

          ② 给线程添加了join()方法,当运行到分线程时主线程停止运行,等待分线程运行结束,然后继续运行主线程

例子2(2)创建一个线程列表,将分线程添加到列表中

import threading
import time

def playGame(gameName):
    print("begin to play %s at %s" % (gameName,time.ctime()))
    time.sleep(3)
    print("stop to  play %s at %s" % (gameName,time.ctime()))

def watchVideo(videoName):
    print("begin to watch videoName %s at %s" % (videoName,time.ctime()))
    time.sleep(5)
    print("stop to watch videoName %s at %s" % (videoName,time.ctime()))

threads = []
t1=threading.Thread(target=playGame,args = ('Wilderness hunter ',))
t2=threading.Thread(target=watchVideo,args = ('Tokyo hot',))
threads.append(t1)
threads.append(t2)

if __name__ == '__main__':

    for t in threads:
        t.start()
    print("ending")

输出:


例子3:setDaemon()方法

    setDaemon()方法默认是False,所以主线程也是默认是等待分线程运行结束,设置为True表示设置守护线程,只要主线程结束则分线程也不再执行

import threading
import time

def playGame(gameName):
    print("begin to play %s at %s" % (gameName,time.ctime()))
    time.sleep(3)
    print("stop to  play %s at %s" % (gameName,time.ctime()))

def watchVideo(videoName):
    print("begin to watch videoName %s at %s" % (videoName,time.ctime()))
    time.sleep(5)
    print("stop to watch videoName %s at %s" % (videoName,time.ctime()))

threads = []
t1=threading.Thread(target=playGame,args = ('Wilderness hunter ',))
t2=threading.Thread(target=watchVideo,args = ('Tokyo hot',))
threads.append(t1)
threads.append(t2)

if __name__ == '__main__':

    for t in threads:
        t.setDaemon(True)
        t.start()
    print("ending")

输出:

提醒:设置线程守护一定要在启动线程之前,否则会报错

猜你喜欢

转载自blog.csdn.net/li_haiyu/article/details/79999465
今日推荐