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 @ 2024-04-25 12:32  低头不见抬头见  阅读(31)  评论(0)    收藏  举报