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

相关推荐
望获linux几秒前
【Linux基础知识系列】第四十三篇 - 基础正则表达式与 grep/sed
linux·运维·服务器·开发语言·前端·操作系统·嵌入式软件
ahead~5 分钟前
【大模型入门】访问GPT_API实战案例
人工智能·python·gpt·大语言模型llm
留不住丨晚霞44 分钟前
说说SpringBoot常用的注解?
java·开发语言
大模型真好玩1 小时前
准确率飙升!GraphRAG如何利用知识图谱提升RAG答案质量(额外篇)——大规模文本数据下GraphRAG实战
人工智能·python·mcp
19891 小时前
【零基础学AI】第30讲:生成对抗网络(GAN)实战 - 手写数字生成
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·近邻算法
华科云商xiao徐1 小时前
Java多线程爬虫动态线程管理实现
java·爬虫·数据挖掘
hardStudy_h1 小时前
C++——内联函数与Lambda表达式
开发语言·jvm·c++
华科云商xiao徐1 小时前
高性能小型爬虫语言与代码示例
前端·爬虫
applebomb1 小时前
没合适的组合wheel包,就自行编译flash_attn吧
python·ubuntu·attention·flash
艾莉丝努力练剑1 小时前
【C语言】学习过程教训与经验杂谈:思想准备、知识回顾(三)
c语言·开发语言·数据结构·学习·算法