您的位置:首页 > 编程语言 > Python开发

python 多线程 threading

2017-09-04 11:32 375 查看
(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 多线程 threading