分程序設定
工具: threading包
1 先寫需要分程序執行的函式或者類
defmaigic
():pass
2 例項化threading,得到新的程序
threadone = threading.thread(target=maigic)
此時還可以接受arg引數
此時整個程式存在三個程序,import threading
import time
defget_html
(): print("get_html is start ")
time.sleep(3)
print("get_html is end")
defget_url
(): print("get_url is start")
time.sleep(2)
print("get_url is end")
thread1 = threading.thread(target=get_html)
thread2 = threading.thread(target=get_url)
start_time = time.time()
print("start time is {}".format(start_time))
thread1.start()
thread2.start()
end_time = time.time()
print("end time is {}".format(end_time-start_time))
>>>
start time is
1532486991.7209127
get_html is start
get_url is start
end time is
0.0004737377166748047
get_url is end
get_html is end
1 主程序:程式執行
2 程序1 : thread1
3 程序2 : thread2
三個程序屬於併發的,主程式會等到其餘兩個程序結束後,主程式才會結束
但是主程式是執行完所有語句然後等待其餘程序的結束,而不是停在程序語句**等待
此時可以設定守護程序--優先順序在主程序之下,主程序可以kill掉守護程序
thread1.setdaemon(true) #將thread2設為守護程序
thread1.join() # 等待程序結束
有了守護程序之後,主程式就會在自身結束後,(不管守護程序還是不是在執行)kill守護程序import threading
import time
defget_html
(): print("get_html is start ")
time.sleep(3)
print("get_html is end")
defget_url
(): print("get_url is start")
time.sleep(2)
print("get_url is end")
thread1 = threading.thread(target=get_html)
thread2 = threading.thread(target=get_url)
thread1.setdaemon(true)
thread2.setdaemon(true) # 將thread2設為守護程序--優先順序在主程序之下,主程序可以kill掉守護程序
start_time = time.time()
print("start time is {}".format(start_time))
thread1.start()
thread2.start()
thread1.join() # 等待程序結束
thread2.join()
end_time = time.time()
print("end time is {}".format(end_time-start_time))
>>>
start time is
1532486991.7209127
get_html is start
get_url is start
get_url is end
get_html is end
end time is
0.0004737377166748047
但是有了 :
主程式就會停在join**,等待結束後,再繼續執行thread1.join() # 等待程序結束
thread2.join()
改進把變成類,自定義類繼承threading.thread 類
此時需要過載run方法 , run方法就是執行邏輯
import threading
import time
class
thread_case
(threading.thread):
def__init__
(self,name,time):
self.mytime = time
self.myname = name
super().__init__(name=name)
defrun(self):
print(" is start ".format(name=self.myname))
time.sleep(self.mytime)
print(" is end".format(name=self.myname))
thread1 = thread_case("get_url",3)
thread2 = thread_case("get_html",2)
thread1.setdaemon(true)
start_time = time.time()
print("start time is {}".format(start_time))
thread1.start()
thread2.start()
# thread1.join() # 等待程序結束
# thread2.join()
end_time = time.time()
print("end time is {}".format(end_time-start_time))
>>>
start time is
1532496367.0786047
get_url is start
get_html is start
end time is
0.0009756088256835938
get_html is end
1 12 python基礎學習
def checki while true x input 請輸入 if x 1 print 當前餘額為 999元 elif x 2 print 當前剩餘流量為 5g elif x 3 print 當前剩餘通話為 189分鐘 elif x 0 print 退出自助查詢系統 break else pr...
JAVA多執行緒之Runnable和Thread比較
在我們開發的過程中常常會碰到多執行緒的問題,對於多執行緒的實現方式主要有兩種 實現runnable介面 繼承thread類。對於這兩種多執行緒的實現方式也是有著一些差異。既然實現了多執行緒那必然離不開管理這些執行緒,當問題比簡單時乙個或者幾個執行緒就ok了,也涉及不到效率問題。一旦執行緒數量多起來的...
python多執行緒 python多執行緒
通常來說,多程序適用於計算密集型任務,多執行緒適用於io密集型任務,如網路爬蟲。關於多執行緒和多程序的區別,請參考這個 下面將使用python標準庫的multiprocessing包來嘗試多執行緒的操作,在python中呼叫多執行緒要使用multiprocessing.dummy,如果是多程序則去掉...