python 多线程 threading

(python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费你和时间,所以我们直接学习threading 就可以了。)


# 单线程 我想做听音乐和看电影两件事儿,那么一定要先排一下顺序。

from time import ctime, sleep

def music():
    for i in range(2):
        print("I was listening to music. %s" % ctime())
        sleep(2)
        
def move():
    for i in range(2):
        print("I was at the movies! %s" % ctime())
        sleep(5)
        
if __name__ == '__main__':
    music()
    move()
    print("all over %s" % ctime())


=========================== RESULT ================================

I was listening to music. Mon Sep  4 11:00:08 2017
I was listening to music. Mon Sep  4 11:00:10 2017
I was at the movies! Mon Sep  4 11:00:12 2017
I was at the movies! Mon Sep  4 11:00:17 2017
all over Mon Sep  4 11:00:22 2017


# 单线程 music()和move()更应该被看作是音乐和视频播放器,至于要播放什么歌曲和视频应该由我们使用时决定

import threading
from time import ctime, sleep

def music(func):
    for i in range(2):
        print("I was listening to %s. %s" % (func, ctime()))
        sleep(2)

def move(func):
    for i in range(2):
        print("I was at the %s! %s" % (func, ctime()))
        sleep(5)

if __name__ == '__main__':
    music("一生所爱")
    move("敦刻尔克")
    print("all over %s" % ctime())


=========================== RESULT ================================

I was listening to 一生所爱. Mon Sep  4 11:12:39 2017
I was listening to 一生所爱. Mon Sep  4 11:12:40 2017
I was at the 敦刻尔克! Mon Sep  4 11:12:42 2017
I was at the 敦刻尔克! Mon Sep  4 11:12:47 2017
all over Mon Sep  4 11:12:52 2017


# 多线程 引入threadring来同时播放音乐和视频

import threading
from time import ctime, sleep


def music(func):
    for i in range(2):
        print("I was listening to %s. %s" % (func, ctime()))
        sleep(2)


def move(func):
    for i in range(2):
        print("I was at the %s! %s" % (func, ctime()))
        sleep(5)

threads = []
t1 = threading.Thread(target=music, args=("一生所爱",))
threads.append(t1)
t2 = threading.Thread(target=move, args=("敦刻尔克",))
threads.append(t2)


if __name__ == '__main__':
    for t in threads:
        t.setDaemon(True)
        t.start()

    print("all over %s" % ctime())


=========================== RESULT ================================

I was listening to 一生所爱. Mon Sep  4 11:21:01 2017
I was at the 敦刻尔克! Mon Sep  4 11:21:01 2017
all over Mon Sep  4 11:21:01 2017


注释:从执行结果来看,子线程(muisc 、move )和主线程(print "all over %s" %ctime())都是同一时间启动,但由于主线程执行完结束,所以导致子线程也终止。



# 多线程 继续调整程序
# 对上面的程序加了个join()方法,用于等待线程终止。join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞
# join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程


import threading
from time import ctime, sleep


def music(func):
    for i in range(2):
        print("I was listening to %s. %s" % (func, ctime()))
        sleep(2)


def move(func):
    for i in range(2):
        print("I was at the %s! %s" % (func, ctime()))
        sleep(5)

threads = []
t1 = threading.Thread(target=music, args=("一生所爱",))
threads.append(t1)
t2 = threading.Thread(target=move, args=("敦刻尔克",))
threads.append(t2)


if __name__ == '__main__':
    for t in threads:
        t.setDaemon(True)
        t.start()

    t.join()

    print("all over %s" % ctime())


=========================== RESULT ================================

I was listening to 一生所爱. Mon Sep  4 11:24:56 2017
I was at the 敦刻尔克! Mon Sep  4 11:24:56 2017
I was listening to 一生所爱. Mon Sep  4 11:24:58 2017
I was at the 敦刻尔克! Mon Sep  4 11:25:01 2017
all over Mon Sep  4 11:25:06 2017


注:更详细的使用请参考其它文档或资料

https://docs.python.org/3/library/threading.html

https://docs.python.org/2/library/threading.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tigerking1017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值