python 一个开箱即用的多线程代码
适合数据采集的多线程代码!
# coding=utf-8
# coding=gbk
# coding=gb2312
import requests
import threading
import queue
def get_response(source):
return response
def decode(source, response):
##保存数据
pass
def mains():
while True:
try:
source = q.get(block=True, timeout=30)
except queue.Empty:
break
else:
pass
if not source:
break
try:
response = get_response(source)
if res:
decode(source, response)
else:
print(f"source{source}为None")
except Exception as e:
continue
return
def start_():
thlist = list()
# 开启多线程
ths = 50
for i in range(ths):
th = threading.Thread(target=mains)
thlist.append(th)
for i in range(ths):
thlist[i].start()
for i in range(ths):
thlist[i].join()
def get_src():
return source_list
if __name__ == '__main__':
q =queue.Queue()
## 数据源
source_list = get_src()
for source in source_list:
q.put(source)
start_()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库