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_()


posted @   低头不见抬头见  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
点击右上角即可分享
微信分享提示