使用 Python 多线程时属性错误
Attribute error when using Python multithreading
我收到一个错误:
AttributeError: 'Park_Stat_Thread' object has no attribute 'park_stat
当我运行下面的脚本。知道如何传回 park_stat
变量吗?
如您所见,我使用多线程从 Web 抓取 URL 列表:
class Park_Stat_Thread(Thread):
def __init__(self, thread_num):
super().__init__()
self.thread_num = thread_num
def run(self):
print('starting thread %s.' % self.thread_num)
process_queue(self)
print('exiting thread %s.' % self.thread_num)
def process_queue(self):
while True:
try:
x = my_queue.get(block=False)
except queue.Empty:
return
else:
parking_stat_getter(self,x)
time.sleep(1)
def parking_stat_getter(self,x):
base = 'http://www.ahuzot.co.il/Parking/ParkingDetails/?ID={}'
response = requests.get(base.format(self.thread_num))
soup = BeautifulSoup(response.text, "lxml")
try:
self.park_stat = 0
for img in soup.find_all('img' , attrs={'src': re.compile("ParkingIcons")}):
soup_img = str(img)
if 'male' in soup_img:
self.park_stat=1
elif 'meat' in soup_img:
self.park_stat=2
elif 'panui' in soup_img:
self.park_stat=3
except AttributeError:
self.park_stat = np.nan
return self.park_stat
def get_parkings_Stat(parking_id_lists):
threads = [Park_Stat_Thread(t) for t in range(1, 8)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
park_details = dict(zip(parking_id_lists, [thread.park_stat for thread in threads]))
return park_details
您缺少 class 构造函数中的 class 属性。您不能稍后声明 class 属性,但可以将其设置为某个虚拟值。
class Park_Stat_Thread(Thread):
def __init__(self, thread_num):
super().__init__()
self.thread_num = thread_num
self.park_stat = 0
def run(self):
print('starting thread %s.' % self.thread_num)
process_queue(self)
print('exiting thread %s.' % self.thread_num)
def process_queue(self):
while True:
try:
x = my_queue.get(block=False)
except queue.Empty:
return
else:
parking_stat_getter(self,x)
time.sleep(1)
def parking_stat_getter(self,x):
base = 'http://www.ahuzot.co.il/Parking/ParkingDetails/?ID={}'
response = requests.get(base.format(self.thread_num))
soup = BeautifulSoup(response.text, "lxml")
try:
self.park_stat = 0
for img in soup.find_all('img' , attrs={'src': re.compile("ParkingIcons")}):
soup_img = str(img)
if 'male' in soup_img:
self.park_stat=1
elif 'meat' in soup_img:
self.park_stat=2
elif 'panui' in soup_img:
self.park_stat=3
except AttributeError:
self.park_stat = np.nan
return self.park_stat
def get_parkings_Stat(parking_id_lists):
threads = [Park_Stat_Thread(t) for t in range(1, 8)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
park_details = dict(zip(parking_id_lists, [thread.park_stat for thread in threads]))
return park_details
我收到一个错误:
AttributeError: 'Park_Stat_Thread' object has no attribute 'park_stat
当我运行下面的脚本。知道如何传回 park_stat
变量吗?
如您所见,我使用多线程从 Web 抓取 URL 列表:
class Park_Stat_Thread(Thread):
def __init__(self, thread_num):
super().__init__()
self.thread_num = thread_num
def run(self):
print('starting thread %s.' % self.thread_num)
process_queue(self)
print('exiting thread %s.' % self.thread_num)
def process_queue(self):
while True:
try:
x = my_queue.get(block=False)
except queue.Empty:
return
else:
parking_stat_getter(self,x)
time.sleep(1)
def parking_stat_getter(self,x):
base = 'http://www.ahuzot.co.il/Parking/ParkingDetails/?ID={}'
response = requests.get(base.format(self.thread_num))
soup = BeautifulSoup(response.text, "lxml")
try:
self.park_stat = 0
for img in soup.find_all('img' , attrs={'src': re.compile("ParkingIcons")}):
soup_img = str(img)
if 'male' in soup_img:
self.park_stat=1
elif 'meat' in soup_img:
self.park_stat=2
elif 'panui' in soup_img:
self.park_stat=3
except AttributeError:
self.park_stat = np.nan
return self.park_stat
def get_parkings_Stat(parking_id_lists):
threads = [Park_Stat_Thread(t) for t in range(1, 8)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
park_details = dict(zip(parking_id_lists, [thread.park_stat for thread in threads]))
return park_details
您缺少 class 构造函数中的 class 属性。您不能稍后声明 class 属性,但可以将其设置为某个虚拟值。
class Park_Stat_Thread(Thread):
def __init__(self, thread_num):
super().__init__()
self.thread_num = thread_num
self.park_stat = 0
def run(self):
print('starting thread %s.' % self.thread_num)
process_queue(self)
print('exiting thread %s.' % self.thread_num)
def process_queue(self):
while True:
try:
x = my_queue.get(block=False)
except queue.Empty:
return
else:
parking_stat_getter(self,x)
time.sleep(1)
def parking_stat_getter(self,x):
base = 'http://www.ahuzot.co.il/Parking/ParkingDetails/?ID={}'
response = requests.get(base.format(self.thread_num))
soup = BeautifulSoup(response.text, "lxml")
try:
self.park_stat = 0
for img in soup.find_all('img' , attrs={'src': re.compile("ParkingIcons")}):
soup_img = str(img)
if 'male' in soup_img:
self.park_stat=1
elif 'meat' in soup_img:
self.park_stat=2
elif 'panui' in soup_img:
self.park_stat=3
except AttributeError:
self.park_stat = np.nan
return self.park_stat
def get_parkings_Stat(parking_id_lists):
threads = [Park_Stat_Thread(t) for t in range(1, 8)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
park_details = dict(zip(parking_id_lists, [thread.park_stat for thread in threads]))
return park_details