线程继承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)
posted @ 2022-04-23 22:24  狒狒桑  阅读(47)  评论(0)    收藏  举报