"""
方法一:创建函数并且传入Thread 对象中 ,适用小程序
"""
import time
def get_detail_html(url):
print(url)
time.sleep(2)
print('thread1 end')
thread1 = threading.Thread(target=get_detail_html,args=("",))
# thread1.setDaemon(True)#设置线程为守护线程,当主线程退出,守护线程直接退出
thread1.start()
thread1.join()#阻塞主线程,等待子线程完成才会运行主线程
"""
方法二:继承threading.Thread,重写run方法
适用复杂程序,可以在自定义类里增加复杂逻辑
"""
class GetDetailHtml(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
time.sleep(2)
print('thread1 end')
thread1 = GetDetailHtml()
thread1.start()
thread1.join()