Python 单线程 多线程

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

#coding=utf-8
import threading
from time import ctime,sleep,clock

def 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-8
import threading
from time import ctime,sleep,clock

def 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()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值