python-多线程

进程:是程序的一次执行,有独立的内存地址空间。

线程:是进程的分支,用于完成和实现进程的功能,线程之间共享内存地址空间

1:单线程,一次只能做一件事情,比如看电影和听音乐,只有它们中的一个结束了,才能继续下一件事情:

 ODW [:/srv/scratch/python-scripts] 1024$ cat  music-movie.py

from time import sleep,ctime                                //导入时间相关的模块方法

def music(args):                                       //定义for循环打印输出2次,每次间隔1s
        for i in range(2):
                print ("i am listening the music--%s at%s"%(args,ctime()))
                sleep(1)
def movie(args):                                       //同上
        for t in range(2):
                print ("i am watching the movies --%s at%s"%(args,ctime()))
                sleep(5)

if __name__=="__main__":                    //判断该脚本文件时候是直接执行还是被调用 ,直接执行就会调用music和movie函数,
       music("rolling in the deep")
        movie("the iron-wolf")

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

ODW [:/srv/scratch/python-scripts] 1023$ python3 music-movie.py
i am listening the music--rolling in the deep at Tue Aug  1 17:31:49 2017 //由时间可以看出是一件一件按部就班的执行的
i am listening the music--rolling in the deep at Tue Aug  1 17:31:50 2017
i am watching the movies--the iron-wolf  at Tue Aug  1 17:31:51 2017
i am watching the movies--the iron-wolf  at Tue Aug  1 17:31:56 2017
all over Tue Aug  1 17:32:01 2017

2:多线程,同一时刻可以有多个线程同时执行:

 ODW [:/srv/scratch/python-scripts] 1054$ cat  music-movie1.py
import threading
from time import sleep,ctime

def music(args):
        for i in range(2):
                print ("i am listening the music--%s at %s"%(args,ctime()))
                sleep(1)
def movie(args):
        for t in range(2):
                print ("i am watching the movies--%s  at %s"%(args,ctime()))
                sleep(5)
threads=[]          //定义一个列表用来放置线程对象
t1=threading.Thread(target=music,args=("rolling in the deep",)) //创建对象,指定引用函数和参数,要注意args=(,)中的逗号,当传入一个参数,不加逗号报错
threads.append(t1)    //向列表中添加数据对象t1,t2
t2=threading.Thread(target=movie,args=("the iron-wolf",))
threads.append(t2)
if __name__=="__main__":
    for t in threads:
        t.setDaemon(True)  //setDaemon属性的设置必须在线程启动之前,(默认这个属性为False,主进程会等待守护子进程运行结束。)

        t.start()                           //当设置为True时声明为守护线程,该线程不重要,主线程运行完成,不管子线程是否运行结束,都随主线程强制结束。
print ("all over %s"%ctime())

 ODW [:/srv/scratch/python-scripts] 1053$ python3 music-movie1.py
i am listening the music--rolling in the deep at Tue Aug  1 17:57:39 2017  //由函数定义可知线程需要有等待时间和打印的次数,但是输出并没有显示,说明子线程并没有运行
i am watching the movies--the iron-wolf  at Tue Aug  1 17:57:39 2017       // 结束而被强制终止,
all over Tue Aug  1 17:57:39 2017
3.只更改setDaemon的属性值为默认值False查看输出:

 ODW [:/srv/scratch/python-scripts] 1056$ python3 music-movie1.py 
i am listening the music--rolling in the deep at Tue Aug  1 18:18:19 2017
i am watching the movies--the iron-wolf  at Tue Aug  1 18:18:19 2017
all over Tue Aug  1 18:18:19 2017  //由输出可知,主线程和子线程同时启动,并且主线程会守护子线程的输出直至结束
i am listening the music--rolling in the deep at Tue Aug  1 18:18:20 2017
i am watching the movies--the iron-wolf  at Tue Aug  1 18:18:24 2017

4在第2个脚本的基础上加jion方法:                                //此时setDeamon=True, join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个线程都

ODW [:/srv/scratch/python-scripts] 1063$ python3 music-movie1.py     //结束了才执行主线程
i am listening the music--rolling in the deep at Tue Aug  1 18:37:422017   // join用于等待线程终止的作用是在子线程完成运行之前,这个子线程的父线程将
i am watching the movies--the iron-wolf  at Tue Aug  1 18:37:42 2017           //一直被阻塞
i am listening the music--rolling in the deep at Tue Aug  1 18:37:43 2017    //join方法用于等待线程终止
i am watching the movies--the iron-wolf  at Tue Aug  1 18:37:47 2017
all over Tue Aug  1 18:37:52 2017
5僵尸进程:

cat  music-movie1.py

import threading
from time import sleep,ctime

def music(args):
        for i in range(2):
                print ("i am listening the music--%s at %s"%(args,ctime()))
                sleep(5)
def movie(args):
        for t in range(2):
                print ("i am watching the movies--%s  at %s"%(args,ctime()))
                sleep(2)
threads=[]
t1=threading.Thread(target=music,args=("rolling in the deep",))
threads.append(t1)
t2=threading.Thread(target=movie,args=("the iron-wolf",))
threads.append(t2)
if __name__=="__main__":
        for t in threads:
                t.setDaemon(True)
                t.start()
        t.join()
print ("all over %s"%ctime())

ODW [:/srv/scratch/python-scripts] 1067$ python3 music-movie1.py    //仅仅只是修改2个函数的sleep时间
i am listening the music--rolling in the deep at Tue Aug  1 18:52:51 2017   //2个线程同时启动,主线程和movie线程都结束了,但是music这个线程没有结束,
i am watching the movies--the iron-wolf  at Tue Aug  1 18:52:51 2017    //
i am watching the movies--the iron-wolf  at Tue Aug  1 18:52:53 2017
all over Tue Aug  1 18:52:55 2017




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值