首页 > 编程语言 >python 使用thread多线程执行耗时代码

python 使用thread多线程执行耗时代码

时间:2022-12-28 17:11:34浏览次数:42  
标签:thread python self list queue num 多线程 out

python 使用thread多线程执行耗时代码

1、引入所需要的包

import queue
import threading
import traceback

2、定义线程类:

class ThreadService(threading.Thread):
   def __init__(self, queue, out_dict):
       super(ThreadService, self).__init__()

       self.queue = queue
       self.out_dict = out_dict

   def run(self):
       while True:
           try:
               data = 耗时的方法(self.queue)
               self.out_dict[self.queue] = data
           except queue.Empty:
               break
           except:
               print(traceback.format_exc())
               break

   @classmethod
   def cal_thread_num(cls, task_num, per_thread_num=5, max_thread_num=200):
       if task_num <= per_thread_num:
           return 1
       return min(task_num, int(task_num / per_thread_num), max_thread_num)

3、定义调用线程类


class LoadBundleService(object):

   @classmethod
   def loard(cls, req_list):
       out_dict = {}
       path_queue = queue.Queue()
       for v in req_list:
           path_queue.put(v)

       thread_list = []
       thread_num = ThreadService.cal_thread_num(task_num=len(req_list))
       for i in range(thread_num):
           t = ThreadService(path_queue)
           thread_list.append(t)

       # 启动线程
       for t in thread_list:
           t.start()

       # 等待线程结果
       for t in thread_list:
           t.join()

       # 输出结果
       return thread_list[0].out_dict
 

标签:thread,python,self,list,queue,num,多线程,out
From: https://www.cnblogs.com/yangbeita/p/17010727.html

相关文章

  • Python对象的比较和拷贝
    Python对象的比较和拷贝本文内容存在主观理解,详细请查阅官网文档比较(==VSis)==操作符是比较对象的值是否相等,而is比较的事对象的身份标识是否相等,即它们是否是同一......
  • tensorflow_probability.python.bijectors的一些使用
      网上见到一个TensorFlow的代码,没见过这个形式的,是概率编程的代码:#coding=utf-8#Copyright2020TheTF-AgentsAuthors.##LicensedundertheApacheLicens......
  • python中global 和 nonlocal 的作用域
    python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量。一globalglobal关键字用来在函数或其他局部作用域中使用全局变量。......
  • 【leetcode】3: 无重复字串的最长子串(python)
    给定一个字符串s,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1:输入:s="abcabcbb"输出:3解释:因为无重复字符的最长子串是"abc",所以其长度为3......
  • [oeasy]python0033_回车_carriage_return_figlet_字体变大
    回到开头回忆上次内容进程前后台切换<kbd>ctrl</kbd>+<kbd>z</kbd>把当前进程切换到后台并暂停​​jobs​​查看所有作业用​​fg​​可以把后台进程再切回前台​​......
  • python logging配置
    python中,logging由logger,handler,filter,formater四个部分组成。logger是提供我们记录日志的方法;handler是让我们选择日志的输出地方,如:控制台,文件,邮件发送等,一个logger添加......
  • python中的mysql操作教程及实例
    一.数据库在自动化测试中的应用存测试数据有的时候大批量的数据,我们需要存到数据库中,在测试的时候才能用到,测试的时候就从数据库中读取出来。这点是非常重要的!存测试结......
  • 数值计算:前向和反向自动微分(Python实现)
    1自动微分我们在《数值分析》课程中已经学过许多经典的数值微分方法。许多经典的数值微分算法非常快,因为它们只需要计算差商。然而,他们的主要缺点在于他们是数值的,这意味......
  • python中的集合推导式
    集合推导式可用来去重需求:将列表list1=[2,2,2,3,4,4,4]中的偶数进行筛选,并且去重list1=[2,2,2,3,4,4,4]set1={iforiinlist1ifi%2==0}print(set......
  • python中的列表推导式
    1.单列表,单条件求1-20之间的偶数list1=[]foriinrange(1,21):ifi%2==0:list1.append(i)print(list1)列表推导式list2=[iforiinrange(1,21)if......