Queue的多线程爬虫和multiprocessing多进程

**Queue的模块里面提供了同步的、线程安全的队列类,包括FIFO(先入后出)队列Queue、FIFO(后入先出)LifoQueue和优先队列PriorityQueue。(在上个文件创建了爬取文件)**我们使用这个方法来获取,代码如下:

python 复制代码
import threading
import requests
import time
import queue as Queue

link_list=[]
with open('alexa.tex','r')as file:
    file_list=file.readlines()
    for eachone in file_list:
        link=eachone.split('\t')[1]
        link=link.replace('\n','')
        link_list.append(link)
start=time.time()
class myThread(threading.Thread):
    def __init__(self,name,q):
        threading.Thread.__init__(self)
        self.name=name
        self.q=q
    def run(self):
        print('Starting'+self.name)
        while True:
            try:
                crawler(self.name,self.q)
            except:
                break
            print('Exiting'+self.name)
def crawler(threadName,q):
    url=q.get(timeout=2)
    try:
        r=requests.get(url,timeout=20)
        print(q.qsize(),threadName,r.status_code,url)
    except Exception as e:
        print(q.qsize(),threadName,url,'Error:',e)


aii_list=['Thread-1','Thread-2','Thread-3','Thread-4','Thread-5']

workQueue=Queue.Queue(1000)
thread=[]

#建立新的线程
for thName in aii_list:
    thread=myThread(thName,workQueue)
    thread.start()
    aii_list.append(thread)

#填充列表
for i in link_list:
    workQueue.put(link_list)

#结束线程
for t in thread:
    thread.join()

end=time.time()
print('当前的总时间:',end-start)
print('Exiting')

对象传入myThread中;

thread = myThread(tName,workQueue)

使用一个for循环来实现:

for url in link_list=:

work.Queue.put(url)

多进程:

使用multiprocess库有两种方法:1.Process+Queue的方法 2.Pool+Queue的方法

我们因先了解计算机的cpu的核心:

python 复制代码
from multiprocessing import cpu_count
print(cpu_count())

然后代码示例:

python 复制代码
from multiprocessing import Process,Queue
import requests
import time


link_list=[]
with open('alexa.tex','r')as file:
    file_list=file.readlines()
    for eachone in file_list:
        link=eachone.split('\t')[1]
        link=link.replace('\n','')
        link_list.append(link)
start=time.time()
class myProcess(Process):
    def __init__(self,q):
        Process.__init__(self)
        self.q=q
    def run(self):
        print('Starting'+self.name)
        while True:
            try:
                crawler(self.name,self.q)
            except:
                break
            print('Exiting'+self.name)
def crawler(q):
    url=q.get(timeout=2)
    try:
        r=requests.get(url,timeout=20)
        print(q.qsize(),r.status_code,url)
    except Exception as e:
        print(q.qsize(),url,'Error:',e)

if __name__ == '__main__':
    ProcessNames=['prcess1','prcess2','prcess3']
    workQueue=Queue(1000)
    
    for url in link_list:
        workQueue.put(url)
        
    for i in range(0,3):
        p=myProcess(workQueue)
        p.daemon=True
        p.start()
        p.join()
            
    end=time.time()
    print('当前的总时间:',end-start)
    print('Exiting')

与多线程相比多进程相比,多进程里面设置了:(当父进程结束后,子进程就会自动被终止)

p.daemon=Ture

并且multprocessing自带了Queue

相关推荐
tang7778941 分钟前
爬虫如何绕过绕过“5秒盾”Cloudflare:从浏览器指纹模拟到Rust求解之不完全指南
开发语言·爬虫·rust·cloudflare
Yuer202544 分钟前
什么是 Rust 语境下的“量化算子”——一个工程对象的最小定义
开发语言·后端·rust·edca os·可控ai
深蓝电商API44 分钟前
Scrapy爬虫限速与并发控制最佳实践
爬虫·python·scrapy
Derrick__11 小时前
淘宝MD5爬虫
爬虫·python
hqwest1 小时前
码上通QT实战26--系统设置01-系统设置布局
开发语言·qt·qss·qt基础控件·qt布局控件·qt表格控件
薛定谔的猫19821 小时前
llama-index Embedding 落地到 RAG 系统
开发语言·人工智能·python·llama-index
jghhh011 小时前
传递对准MATLAB仿真程序
开发语言·matlab
nimadan122 小时前
**手机小说扫榜工具2025推荐,精准追踪榜单动态与题材风向
python·智能手机
编程武士2 小时前
Python 各版本主要变化速览
开发语言·python
hqwest3 小时前
码上通QT实战29--系统设置04-用户操作管理
开发语言·qt·模态窗体·addbindvalue·bindvalue