Python-学习之路-15 多线程1

2019-03-03  本文已影响0人  末世狂人

多线程

全局解释器(GIL)

Python包

# 多线程 案例1
import time
import _thread as thread

def loop1():
    # ctime 获取当前时间
    print("start loop 1 at :",time.ctime())
    
    #休眠4秒
    time.sleep(4)
    print("end loop 1 at :",time.ctime())
    
def loop2():
    # ctime 获取当前时间
    print("start loop 2 at :",time.ctime())
    
    #休眠4秒
    time.sleep(2)
    print("end loop 2 at :",time.ctime())
    
def main():
    print("start at :",time.ctime())
    thread.start_new_thread(loop1,())
    thread.start_new_thread(loop2,())
    print("all done at :",time.ctime())
    
if __name__ == '__main__':
    main()
start at : Sun Mar  3 22:41:12 2019
all done at : Sun Mar  3 22:41:12 2019
start loop 2 at : Sun Mar  3 22:41:12 2019
start loop 1 at : Sun Mar  3 22:41:12 2019
end loop 2 at : Sun Mar  3 22:41:14 2019
end loop 1 at : Sun Mar  3 22:41:16 2019
# 多线程 案例2
import time
import _thread as thread

def loop1(int1):
    # ctime 获取当前时间
    print("start loop 1 at :",time.ctime())
    print("我是参数:",int1)
    #休眠4秒
    time.sleep(4)
    print("end loop 1 at :",time.ctime())
    
def loop2(int1, int2):
    # ctime 获取当前时间
    print("start loop 2 at :",time.ctime())
    print("我是参数1:{0},我是参数2:{1}".format(int1,int2))
    #休眠4秒
    time.sleep(2)
    print("end loop 2 at :",time.ctime())
    
def main():
    print("start at :",time.ctime())
    thread.start_new_thread(loop1,("王老大",))
    thread.start_new_thread(loop2,("张三","李四"))
    print("all done at :",time.ctime())
    
if __name__ == '__main__':
    main()
start at : Sun Mar  3 22:43:48 2019
all done at : Sun Mar  3 22:43:48 2019
start loop 2 at : Sun Mar  3 22:43:48 2019
我是参数1:张三,我是参数2:李四
start loop 1 at : Sun Mar  3 22:43:48 2019
我是参数: 王老大
end loop 2 at : Sun Mar  3 22:43:50 2019
end loop 1 at : Sun Mar  3 22:43:52 2019
# 多线程 案例3
import time
import threading

def loop1(int1):
    # ctime 获取当前时间
    print("start loop 1 at :",time.ctime())
    print("我是参数:",int1)
    #休眠4秒
    time.sleep(4)
    print("end loop 1 at :",time.ctime())
    
def loop2(int1, int2):
    # ctime 获取当前时间
    print("start loop 2 at :",time.ctime())
    print("我是参数1:{0},我是参数2:{1}".format(int1,int2))
    #休眠4秒
    time.sleep(2)
    print("end loop 2 at :",time.ctime())
    
def main():
    print("start at :",time.ctime())
    t1 = threading.Thread(target=loop1, args=("王小虎",))
    t2 = threading.Thread(target=loop2, args=("熊大","熊二"))
    t1.start()
    t2.start()
    
    t1.join()
    t2.join()
    print("all done at :",time.ctime())
    
if __name__ == '__main__':
    main()
start at : Sun Mar  3 22:53:12 2019
start loop 1 at : Sun Mar  3 22:53:12 2019
我是参数: 王小虎
start loop 2 at : Sun Mar  3 22:53:12 2019
我是参数1:熊大,我是参数2:熊二
end loop 2 at : Sun Mar  3 22:53:14 2019
end loop 1 at : Sun Mar  3 22:53:16 2019
all done at : Sun Mar  3 22:53:16 2019
# 守护线程 案例1
import time
import threading

def fun():
    print("start fun")
    time.sleep(2)
    print("end fun")
    
print("Main start")
t = threading.Thread(target= fun, args=())
t.setDaemon(True)
t.start()
time.sleep(1)
print("Main end")
Main start
start fun
Main end
end fun

线程的当用属性

直接继承自threading.Thread

上一篇下一篇

猜你喜欢

热点阅读