Python 单线程 多线程

来源:互联网 发布:python 爬虫 动态页面 编辑:程序博客网 时间:2024/06/05 07:06

早期的操作系统处理问题都是单任务的,同时只能执行一个任务,按顺序执行。

#coding=utf-8import threadingfrom time import ctime,sleep,clockdef music(func):    for i in range(2):        print "I was listening to %s. %s" %(func,ctime())        sleep(1)def movie(func):    for i in range(2):        print "I was at the %s! %s" %(func,ctime())        sleep(5)        def surfInternet(func):    for i in range(2):        print "I was at the %s! %s" %(func,ctime())        sleep(4)        def talk(func):    for i in range(2):        print "I was at the %s! %s" %(func,ctime())        sleep(3)        if __name__ == '__main__':    start = clock()    music(u'南方姑娘')    movie(u'战狼2')    surfInternet(u'阿里巴巴')    talk(u'使用wechat')    end = clock()    print "all over %s" %ctime()    print "total time is %s" %(end-start)


执行结果:
I was listening to 南方姑娘. Wed Aug 30 15:35:15 2017
I was listening to 南方姑娘. Wed Aug 30 15:35:16 2017
I was at the 战狼2! Wed Aug 30 15:35:17 2017
I was at the 战狼2! Wed Aug 30 15:35:22 2017
I was at the 阿里巴巴! Wed Aug 30 15:35:27 2017
I was at the 阿里巴巴! Wed Aug 30 15:35:31 2017
I was at the 使用wechat! Wed Aug 30 15:35:35 2017
I was at the 使用wechat! Wed Aug 30 15:35:38 2017
all over Wed Aug 30 15:35:41 2017
total time is 26.0033179014


#coding=utf-8import threadingfrom time import ctime,sleep,clockdef music(func):    for i in range(2):        print "I was listening to %s. %s" %(func,ctime())        sleep(1)def movie(func):    for i in range(2):        print "I was at the %s! %s" %(func,ctime())        sleep(5)        def surfInternet(func):    for i in range(2):        print "I was at the %s! %s" %(func,ctime())        sleep(4)        def talk(func):    for i in range(2):        print "I was at the %s! %s" %(func,ctime())        sleep(3)threads = []t1 = threading.Thread(target=music,args=(u'南方姑娘',))threads.append(t1)t2 = threading.Thread(target=movie,args=(u'战狼2',))threads.append(t2)t3 = threading.Thread(target=surfInternet,args=(u'阿里巴巴',))threads.append(t3)t4 = threading.Thread(target=talk,args=(u'使用wechat',))threads.append(t4)if __name__ == '__main__':    start = clock()    for t in threads:        t.setDaemon(True)        t.start()     t.join()#在子线程完成运行之前,这个子线程的父线程将一直被阻塞。    end = clock()    print "all over %s" %ctime()    print "total time is %s" %(end-start)
执行结果:
I was listening to 南方姑娘. Wed Aug 30 15:36:41 2017
I was at the 战狼2! Wed Aug 30 15:36:41 2017
I was at the 阿里巴巴! Wed Aug 30 15:36:41 2017
I was at the 使用wechat! Wed Aug 30 15:36:41 2017
I was listening to 南方姑娘. Wed Aug 30 15:36:42 2017
I was at the 使用wechat! Wed Aug 30 15:36:44 2017
I was at the 阿里巴巴! Wed Aug 30 15:36:45 2017
I was at the 战狼2! Wed Aug 30 15:36:46 2017
all over Wed Aug 30 15:36:47 2017
total time is 6.00455547207


从单线程的26s,减少到多线程的6s,节约20s时间。


threads = []

t1 = threading.Thread(target=music,args=(u'南方姑娘',))

threads.append(t1)

创建了threads数组,创建线程t1,使用threading.Thread()方法,在这个方法中调用music方法target=music,args方法对music进行传参。 把创建好的线程t1装到threads数组中。接着以同样的方式创建线程t2,t3,t4,并把t2,t3,t4也装到threads数组。

for t in threads:

t.setDaemon(True)

t.start()

最后通过for循环遍历数组。(数组被装载了t1,t2,t3和t4四个线程)

setDaemon()

  setDaemon(True)将线程声明为守护线程,必须在start() 方法调用之前设置,如果不设置为守护线程程序会被无限挂起。子线程启动后,父线程也继续执行下去,当父线程执行完最后一条语句print "all over %s" %ctime()后,没有等待子线程,直接就退出了,同时子线程也一同结束。

start()

开始线程活动。

join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞。