爬虫-engine

  1. 线程池如何实现engine
  2. ayncio如何实现engine

线程池engine实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import time
import threading
from queue import Queue
queue=Queue()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
base_path="archives"

class WorkThread(threading.Thread):

def __init__(self,name,queue):
threading.Thread.__init__(self)
self.name=name
self.queue=queue

def run(self):
while True:
url,file_img_path=self.queue.get()
print(f"fetch url==>{url} path==>{file_img_path}")
self.download(url,file_img_path)
if queue.empty():
print(f"任务做完,{self.name} 休息中")
time.sleep(2)

def download(self,img_url,file_img_path):
img_response = requests.get(img_url, headers=headers)
with open(file_img_path, "wb") as f:
f.write(img_response.content)


def engine():
global queue
thread_pool=[]
for i in range(10):
t = WorkThread(f"Thread {i}",queue)
t.start()
thread_pool.append(t)