生活随笔
收集整理的這篇文章主要介紹了
Python系统学习第二十四课
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
多線程 VS 多進程
- 程序:一堆代碼以文本形式存入一個文檔
- 進程:程序運行的一個狀態
- 包含地址空間,內存,數據棧
- 每個進程有自己完全獨立的運行環境,多進程共享數據是一個問題
- 線程
- 一個進程的獨立運行片段,一個進程可以有多個線程
- 輕量化的進程
- 一個進程的多個線程間共享數據和上下文運行環境
- 共享互斥問題
- 全局解釋器鎖(GIL)
- python代碼的執行是由python虛擬機進行控制
- 在主循環中只能有一個控制線程在執行
- python包
- thread:有問題,不好用,python3版本改成了_thread
- threading:通行的包
'''
利用time函數,生成兩個函數
順序調用
計算總的運行時間'''
import time
def loop1():print('Start loop 1 at :', time
.ctime
())time
.sleep
(4)print('End loop 1 at:', time
.ctime
())def loop2():print('Start loop 2 at :', time
.ctime
())time
.sleep
(2)print('End loop 2 at:', time
.ctime
())def main():print("Starting at:", time
.ctime
())loop1
()loop2
()print("All done at:", time
.ctime
())if __name__
== '__main__':main
()
Starting at: Tue Jan 1 11:08:36 2019
Start loop 1 at : Tue Jan 1 11:08:36 2019
End loop 1 at: Tue Jan 1 11:08:40 2019
Start loop 2 at : Tue Jan 1 11:08:40 2019
End loop 2 at: Tue Jan 1 11:08:42 2019
All done at: Tue Jan 1 11:08:42 2019
'''
利用time函數,生成兩個函數
順序調用
計算總的運行時間
使用多線程,縮短總時間,使用_thread'''
import time
import _thread
as thread
def loop1():print('Start loop 1 at :', time
.ctime
())time
.sleep
(4)print('End loop 1 at:', time
.ctime
())def loop2():print('Start loop 2 at :', time
.ctime
())time
.sleep
(2)print('End loop 2 at:', time
.ctime
())def main():print("Starting at:", time
.ctime
())thread
.start_new_thread
(loop1
, ())thread
.start_new_thread
(loop2
, ())print("All done at:", time
.ctime
())if __name__
== '__main__':main
()while True: time
.sleep
(1)
Starting at: Tue Jan 1 11:15:30 2019
All done at: Tue Jan 1 11:15:30 2019
Start loop 1 at : Tue Jan 1 11:15:30 2019
Start loop 2 at : Tue Jan 1 11:15:30 2019
End loop 2 at: Tue Jan 1 11:15:32 2019
End loop 1 at: Tue Jan 1 11:15:34 2019
import time
import _thread
as thread
def loop1(in1
):print('Start loop 1 at :', time
.ctime
())print("我是參數 ",in1
)time
.sleep
(4)print('End loop 1 at:', time
.ctime
())def loop2(in1
, in2
):print('Start loop 2 at :', time
.ctime
())print("我是參數 " ,in1
, "和參數 ", in2
)time
.sleep
(2)print('End loop 2 at:', time
.ctime
())def main():print("Starting at:", time
.ctime
())thread
.start_new_thread
(loop1
,("王老大", ))thread
.start_new_thread
(loop2
,("王大鵬", "王曉鵬"))print("All done at:", time
.ctime
())if __name__
== "__main__":main
()while True:time
.sleep
(10)
- threading的使用
- 直接使用threading.Thread生成Thread實例
- 1.t = threading.Thread(target = xxx, args=(xxx, )) #target是函數名,args是參數
- 2.t.start() #氣動多線程
- 3.t.join():等待多線程執行完成
- 守護線程,deamon
- 如果在程序中將子線程設置成守護線程,則子線程會在主線程結束的時候自動退出
- 一般認為,守護線程不重要或者不允許離開主線程獨立運行
- 守護線程案例能否有效果跟環境相關
- 線程常用屬性
- threading.currentThread:返回當前線程變量
- threading.enumerate:返回一個包含正在運行的線程的list,正在運行的線程指的是線程啟動后,結束前的線程
- threading.ativeCount:返回正在運行的線程數量,效果跟len(threading.enumerate)相同
- thr.setName:給線程設置名字
- thr.getName:得到線程的名字
import time
import threading
def loop1(in1
):print('Start loop 1 at :', time
.ctime
())print("我是參數 ",in1
)time
.sleep
(4)print('End loop 1 at:', time
.ctime
())def loop2(in1
, in2
):print('Start loop 2 at :', time
.ctime
())print("我是參數 " ,in1
, "和參數 ", in2
)time
.sleep
(2)print('End loop 2 at:', time
.ctime
())def main():print("Starting at:", time
.ctime
())t1
= threading
.Thread
(target
=loop1
, args
=("王老大",))t1
.start
()t2
= threading
.Thread
(target
=loop2
, args
=("王大鵬", "王小鵬"))t2
.start
()print("All done at:", time
.ctime
())if __name__
== "__main__":main
()while True:time
.sleep
(10)
import time
import threading
def loop1(in1
):print('Start loop 1 at :', time
.ctime
())print("我是參數 ",in1
)time
.sleep
(4)print('End loop 1 at:', time
.ctime
())def loop2(in1
, in2
):print('Start loop 2 at :', time
.ctime
())print("我是參數 " ,in1
, "和參數 ", in2
)time
.sleep
(2)print('End loop 2 at:', time
.ctime
())def main():print("Starting at:", time
.ctime
())t1
= threading
.Thread
(target
=loop1
, args
=("王老大",))t1
.start
()t2
= threading
.Thread
(target
=loop2
, args
=("王大鵬", "王小鵬"))t2
.start
()t1
.join
()t2
.join
()print("All done at:", time
.ctime
())if __name__
== "__main__":main
()while True:time
.sleep
(10)
import time
import threading
def fun():print("Start fun")time
.sleep
(2)print("end fun")print("Main thread")t1
= threading
.Thread
(target
=fun
, args
=() )
t1
.start
()time
.sleep
(1)
print("Main thread end")
import time
import threading
def fun():print("Start fun")time
.sleep
(2)print("end fun")print("Main thread")t1
= threading
.Thread
(target
=fun
, args
=() )
t1
.setDaemon
(True)
t1
.start
()time
.sleep
(1)
print("Main thread end")
import time
import threading
def loop1():print('Start loop 1 at :', time
.ctime
())time
.sleep
(6)print('End loop 1 at:', time
.ctime
())def loop2():print('Start loop 2 at :', time
.ctime
())time
.sleep
(1)print('End loop 2 at:', time
.ctime
())def loop3():print('Start loop 3 at :', time
.ctime
())time
.sleep
(5)print('End loop 3 at:', time
.ctime
())def main():print("Starting at:", time
.ctime
())t1
= threading
.Thread
(target
=loop1
, args
=( ))t1
.setName
("THR_1")t1
.start
()t2
= threading
.Thread
(target
=loop2
, args
=( ))t2
.setName
("THR_2")t2
.start
()t3
= threading
.Thread
(target
=loop3
, args
=( ))t3
.setName
("THR_3")t3
.start
()time
.sleep
(3)for thr
in threading
.enumerate():print("正在運行的線程名字是: {0}".format(thr
.getName
()))print("正在運行的子線程數量為: {0}".format(threading
.activeCount
()))print("All done at:", time
.ctime
())if __name__
== "__main__":main
()while True:time
.sleep
(10)
import threading
import time
class MyThread(threading
.Thread
):def __init__(self
, arg
):super(MyThread
, self
).__init__
()self
.arg
= arg
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!!!!!!!!")
import threading
from time
import sleep
, ctimeloop
= [4,2]class ThreadFunc:def __init__(self
, name
):self
.name
= name
def loop(self
, nloop
, nsec
):''':param nloop: loop函數的名稱:param nsec: 系統休眠時間:return:'''print('Start loop ', nloop
, 'at ', ctime
())sleep
(nsec
)print('Done loop ', nloop
, ' at ', ctime
())def main():print("Starting at: ", ctime
())t
= ThreadFunc
("loop")t1
= threading
.Thread
( target
= t
.loop
, args
=("LOOP1", 4))t2
= threading
.Thread
( target
= ThreadFunc
('loop').loop
, args
=("LOOP2", 2))t1
.start
()t2
.start
()t1
.join
( )t2
.join
()print("ALL done at: ", ctime
())if __name__
== '__main__':main
()112
總結
以上是生活随笔為你收集整理的Python系统学习第二十四课的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。