继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Python 多线程(01)

饮歌长啸
关注TA
已关注
手记 188
粉丝 46
获赞 152
进程-进程有自己的完全独立的运行环境,多进程共享数据是个问题
线程-一个进程独立运行的片段,一个进程可以有多个线程
全局解释器(GTL)

-python代码的执行是由python虚拟机进行控制
-在主循环中只能有一个控制线程在执行
python包

-thread:之前应用的版本,python3改成了_thread
-threading:先行通行的包

案例

'''
利用time函数生成两个函数,顺序调用,计算总的计算时间
'''
import time
def loop1():
    #ctime得到当前时间
    print("start loop1 at ",time.ctime())
    #sleep
    time.sleep(4)
    print("end loop1 at",time.ctime())
def loop2():
    print("start loop2 at ", time.ctime())
    # sleep
    time.sleep(2)
    print("end loop2 at", time.ctime())
def main():
    print("start at",time.ctime())
    loop1()
    loop2()
    print("all done at ",time.ctime())

if __name__=="__main__":
        main()

    
运行结果如下:
start at Mon Jan 7 09:18:44 2019
start loop1 at Mon Jan 7 09:18:44 2019
end loop1 at Mon Jan 7 09:18:48 2019
start loop2 at Mon Jan 7 09:18:48 2019
end loop2 at Mon Jan 7 09:18:50 2019
all done at Mon Jan 7 09:18:50 2019

案例2(案例1使用多线程)

def loop1():
    #ctime得到当前时间
    print("start loop1 at ",time.ctime())
    #sleep
    time.sleep(4)
    print("end loop1 at",time.ctime())
def loop2():
    print("start loop2 at ", time.ctime())
    # sleep
    time.sleep(2)
    print("end loop2 at", time.ctime())
def main():
    print("start at",time.ctime())
    #启动多线程的意思使用多线程去执行某个函数
    threading.Thread(target=loop1).start()
    threading.Thread(target=loop2).start()
    print("all done at ",time.ctime())

if __name__=="__main__":
        main()

   
   

运行结果如下:
start at Mon Jan 7 09:31:46 2019
start loop1 at Mon Jan 7 09:31:46 2019
start loop2 at Mon Jan 7 09:31:46 2019
all done at Mon Jan 7 09:31:46 2019
end loop2 at Mon Jan 7 09:31:48 2019
end loop1 at Mon Jan 7 09:31:50 2019

案例3(多线程+参数)

import time
import threading
import  _thread as thread
def loop1(in1):
    #ctime得到当前时间
    print("start loop1 at ",time.ctime())
    print("gram1",in1)
    #sleep
    time.sleep(4)
    print("end loop1 at",time.ctime())
def loop2(in1,in2):
    print("start loop2 at ", time.ctime())
    # sleep
    print("gram1",in1,"gram2",in2)

    time.sleep(2)
    print("end loop2 at", time.ctime())
def main():
    print("start at",time.ctime())
    #启动多线程的意思使用多线程去执行某个函数
    #启动多线程的函数为start_new_thread
    #参数有两个一个是需要运行的函数,另一个参数作为元祖使用
    #如果有一个参数时一定要价格逗号
    thread.start_new_thread(loop1,("beijing",))
    thread.start_new_thread(loop2,("tianjin","hangzhou"))
    print("all done at ",time.ctime())

if __name__=="__main__":
        main()
        while True:
            time.sleep(8)

    
运行结果如下:
start at Mon Jan 7 09:50:48 2019
all done at Mon Jan 7 09:50:48 2019
start loop1 at Mon Jan 7 09:50:48 2019
gram1 beijing
start loop2 at Mon Jan 7 09:50:48 2019
gram1 tianjin gram2 hangzhou
end loop2 at Mon Jan 7 09:50:50 2019
end loop1 at Mon Jan 7 09:50:52 2019
threading的使用

-t=threading.Thread(target=XXX,args=(xxx,))
-t.start():启动多线程
-t.join():等待多线程执行完成

案例04

import time
import threading
import  _thread as thread
def loop1(in1):
    #ctime得到当前时间
    print("start loop1 at ",time.ctime())
    print("gram1",in1)
    #sleep
    time.sleep(4)
    print("end loop1 at",time.ctime())
def loop2(in1,in2):
    print("start loop2 at ", time.ctime())
    # sleep
    print("gram1",in1,"gram2",in2)

    time.sleep(2)
    print("end loop2 at", time.ctime())
def main():
    print("start at",time.ctime())
    #启动多线程的意思使用多线程去执行某个函数

    t1=threading.Thread(target=loop1,args=("beijing",))
    t2=threading.Thread(target=loop2, args=("tianjin","hangzhou"))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print("all done at ",time.ctime())

if __name__=="__main__":
        main()

   
运行结果如下:
start at Mon Jan 7 10:06:10 2019
start loop1 at Mon Jan 7 10:06:10 2019
gram1 beijing
start loop2 at Mon Jan 7 10:06:10 2019
gram1 tianjin gram2 hangzhou
end loop2 at Mon Jan 7 10:06:12 2019
end loop1 at Mon Jan 7 10:06:14 2019
all done at Mon Jan 7 10:06:14 2019
守护线程

-如果在程序中将子线程设置成守护线程,则子线程惠子啊主程序结束的时候自动退出
-一般认为守护线程不重要或者不允许离开主线程独立运行
案例5

import time
import threading
import  _thread as thread
def fun():
    print("start fun")
    time.sleep(2)
    print("end fun")
print("main thraed")

t1=threading.Thread(target=fun,args=())
#将t1设置成守护线程
t1.setDaemon(True)
t1.start()
time.sleep(1)
print("main thread dead")

  
运行结果如下:
main thraed
start fun
main thread dead
线程常用的属性

-threading.currentThread:返回当前线程呢个变量
-threading.enumerate:返回一个包含正在运行线程的list
-threading.activeCount:返回正在运行的线程的数量
-thr.setName:给线程设置名字
-thr.getName:得到线程的名字
直接继承自thread.Thread

-直接继承Thread
-重写run函数
-类实例可以直接运行
案例6

import time
import threading
#类需要继承自threading.Thread
class MyThread(threading.Thread):
    def __init__(self,arg):
        super(MyThread, self).__init__()
        self.arg=arg
    #必须重写run函数,是真正执行的功能
    def  run(self):
        time.sleep(2)
        print("the args for this class is {0}".format(self.arg))
for  i  in range(5):
    t=MyThread(i)
    t.start()
    t.join()
print("main thread is done")

    
运行结果如下:
the args for this class is 0
the args for this class is 1
the args for this class is 2
the args for this class is 3
the args for this class is 4
main thread is done
---------------------

原文作者:无敌小熊猫

原文链接:https://blog.csdn.net/qq_41609475/article/details/85990214



打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP