python - Python线程似乎被冻结了
问题描述
我有一个网络爬虫。这是它的代码。
# I truncated this code because of the comment.
from threading import Thread
no_of_threads = int(input("No. of threads : "))
if no_of_threads > 70:
no_of_threads = 70
threads = []
def log(data, thid, info=True):
if info:
# print a information to the user
else:
# inform user about the error
def crawl(t_id):
# Here comes the long-time loop. this will run until there are no items left in the list of URLs to be scraped
while True:
# do some complex operations like getting the title, description, etc.
lines = # a one million links list got from a file
for line in lines:
# Get each link in the list and put in the list
# Iterate through the no. of threads given by user and create a thread
for _ in range(no_of_threads):
t = Thread(target=crawl, args=(_,))
t.start()
threads.append(t)
for thread in threads:
thread.join()
它实例化用户输入的线程数。然后在一个永远的 while 循环中,它正在爬取 URL。
我运行了大约 4 个小时,起初,所有线程都工作,但 4 小时后只有一个线程工作。最后,它也变得不活跃。这是为什么?
解决方案
推荐阅读
- php - 如何在没有php的网页中显示来自数据库的信息
- dolphindb - DolphinDB 中的用户定义函数和流聚合器
- azure - 如何解决 Azure Devops Pipeline 中的 -apptype 缺失错误
- javascript - src/app/koszyk.service.ts(21,28) 中的角度错误:错误 TS2339:“{}”类型上不存在属性“przedmioty”
- node.js - IBM Cloud Foundry 上的节点包依赖项 - 未定义要求/模块(未加载包)
- python - 当仅在浏览器中单击时显示时如何使用python获取重定向位置
- html - CSS:在填充内设置内联元素
- java - 在java中输出文本文件的CaesarCipher程序
- php - 是否可以将开关盒放在while循环中?
- java - 将一行中的列值与上一行中的相同列值与 Spark 中的同一 DataFrame 中的相同列值进行比较