其他分享
首页 > 其他分享> > 线程继承threading.Thread

线程继承threading.Thread

作者:互联网

import threading
import time
import requests


class MyThreading(threading.Thread):
def __init__(self, url):
self.url = url # 给run方法传参,只能通过 self的属性
super().__init__() # 重写__init__方法一定要调用父类方法

def run(self):
for i in range(5):
res = requests.get(self.url)
print(threading.current_thread(), '-----', res.status_code)


s_time = time.time()
for i in range(5):
m1 = MyThreading('http://www.baidu.com')
m1.start()
m1.join()
e_time = time.time()
print(e_time - s_time)

标签:__,Thread,url,self,threading,init,线程,time,m1
来源: https://www.cnblogs.com/bigcoolcool/p/16183857.html