通過例項化thread類
通過繼承threadimport threading
import time
def get_detail_html(url):
print("get detail html started")
time.sleep(2)
print("get detail html end")
def get_detail_url(url):
print("get detail url started")
time.sleep(4)
print("get detail url end")
if __name__ == "__main__":
thread1 = threading.thread(target=get_detail_html, args=("",))
thread2 = threading.thread(target=get_detail_url, args=("",))
# thread1.setdaemon(true) # 設定為守護執行緒,意思是主線程關閉後,也關閉這個守護執行緒
thread2.setdaemon(true)
start_time = time.time()
thread1.start()
thread2.start()
thread1.join() #
thread2.join() # 等待這兩個執行緒執行完成後,再執行主線程
print("last time: {}".format(time.time()-start_time))
import threading
import time
class getdetalhtml(threading.thread):
def __init__(self, name):
super().__init__(name=name)
def run(self):
print("get detail html started")
time.sleep(2)
print("get detail html end")
class getdetalurl(threading.thread):
def __init__(self, name):
super().__init__(name=name)
def run(self):
print("get detail url started")
time.sleep(4)
print("get detail url end")
if __name__ == "__main__":
thread1 = getdetalhtml("get_detail_html")
thread2 = getdetalurl("get_detail_url")
start_time = time.time()
thread1.start()
thread2.start()
thread1.join() #
thread2.join() # 等待這兩個執行緒執行完成後,再執行主線程
print("last time: {}".format(time.time() - start_time))
python 多執行緒程式設計
一 執行緒基礎 1 建立執行緒 thread模組提供了start new thread函式,用以建立執行緒。start new thread函式成功建立後還可以對其進行操作。其函式原型 start new thread function,atgs kwargs 其引數含義如下 args 元組形式的引...
python 多執行緒程式設計
一 執行緒基礎 1 建立執行緒 thread模組提供了start new thread函式,用以建立執行緒。start new thread函式成功建立後還能夠對其進行操作。其函式原型 start new thread function,atgs kwargs 其引數含義例如以下 args 元組形式...
Python多執行緒程式設計
import threading import time deffunc name time.sleep 3 print 子執行緒 s 啟動 threading.current thread name print hello name print 子執行緒 s 結束 threading.curren...