首页 > 编程语言 >python 一个开箱即用的多线程代码

python 一个开箱即用的多线程代码

时间:2024-04-25 12:47:20浏览次数:19  
标签:__ get python source 开箱 多线程 response def

适合数据采集的多线程代码!

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


标签:__,get,python,source,开箱,多线程,response,def
From: https://www.cnblogs.com/feimew/p/18157392

相关文章

  • python读取xls表格中指定列或行范围的数据
    importxlrd#打开Excel文件workbook=xlrd.open_workbook('test01.xls')#获取第一个工作表worksheet=workbook.sheet_by_index(0)#指定的行区域#读取第(row_index_x+1)行中,第(start_cols+1)列至第end_cols列范围的数据start_cols=0#第(start_cols+1)列end_cols......
  • 【学习笔记】Python 使用 matplotlib 画图
    目录安装中文显示折线图、点线图柱状图、堆积柱状图坐标轴断点参考资料本文将介绍如何使用Python的matplotlib库画图,记录一些常用的画图demo代码安装#建议先切换到虚拟环境中pipinstallmatplotlib中文显示新版的matplotlib已经支持字体回退功能,因此可以直接设置......
  • 多线程中frida定位关键线程的方法
    pthread_create会先得到一个pthread_internal_t结构体最后会调用__pthread_internal_add并将返回值赋给pthread_create的第一个参数thread_out__pthread_internal_add会将传入的pthread_internal_t加入到g_thread_list全局线程列表中,然后将传入的pthread_internal_t返回,也......
  • AWS S3 Lambda Python脚本函数执行时报错AttributeError: module ‘PIL‘ has no attr
    背景代码示例如下importPILdefadd_image(self,tag,img,step):summary=Summary()bio=BytesIO()iftype(img)==str:img=PIL.Image.open(img)eliftype(img)==PIL.Image.Image:passelse:img=scipy.misc.......
  • python函数递归
    【递归】递归:是函数嵌套调用的一种特殊形式,也就是在调用一个函数的过程中右直接或是间接的调用到本身,然后一直循环deff1():print('一直是我')f1()f1()#调用本身,会死循环============================上述是直接调用间接调用  ================......
  • 多线程如何和连接池交互获取数据?
    在多线程环境中使用连接池来获取数据库连接时,每个线程都可以从池中请求一个连接,然后使用这个连接来执行数据库操作。由于连接池会管理连接的创建和回收,因此它可以有效地减少数据库连接的创建和关闭次数,从而提高性能。以下是一个简单的例子,展示了如何在多线程环境中使用连接池来获......
  • Python基础-模块和面向对象-shutil、re、bs4、requests模块
    概要:模块自定义模块(已经讲了)内置模块shutilre正则表达式第三方模块requests模块bs4模块面向对象:面向对象(Object-OrientedProgramming),简称OOP,是一种编程范式,它使用“对象”来设计软件,这些对象包含了数据(属性)和可以操作这些数据的方法。面向对象的核心......
  • Python基础-模块、Python操作excel
    模块和面向对象-day9今日概要模块自定义模块内置模块第三方模块[!Caution]这个地方讲的会比较多,而且有的东西网上搜不到!!!自动化办公相关模块--Excel和Word面向对象1.面向对象中的概念对象,什么是对象?比如说我桌子上好多东西,有铅笔、钢笔、圆珠笔,玻......
  • 多线程、分布式处理事务
    分布式事务涉及到多个独立的数据库系统或者多个独立的事务处理,它们需要在一个全局事务中协调一致。这种事务通常用于分布式系统或者微服务架构中,其中不同的服务可能使用不同的数据库。在Python中,实现分布式事务通常需要依赖特定的分布式事务管理器或者框架。例如,可以使用两阶段......
  • 交点法公路计算的一些相关方法函数(Python)
    1#弧度归一化2@staticmethod3defnormalize_radian(radian):4returnradian%tau56#获取坐标差7@staticmethod8defget_coord_diff(start_x,start_y,end_x,end_y):9returnend_x-start_x,end_y-st......