Python 多线程处理任务实例

作者:soul11201 时间:2021-06-25 04:50:05 

美餐每天发一个用Excel汇总的就餐数据,我们把它导入到数据库后,行政办公服务用它和公司内的就餐数据进行比对查重。

初始实现是单线程,和import_records去掉多线程后的部分差不多。

读取Excel数据 —> 发送到行政服务接口

安全起见线上操作放在了晚上进行。运行时发现每条数据导入消耗1s多,晚上十点开始跑这几千条数据想想都让人崩溃。

等着也是干等,下楼转两圈透透气,屋里龌龊的空气让人昏昏沉沉,寒冷让人清醒不少,突然想到为什么不用多线程呢?

第一版多线程和处理业务的程序糅合在了一起,跟屎一样难读。后面两天又抽了点时间重构了几个版本,分离出来一个线程池、迭代器和import_records

清晰不少,但是迭代器被暴露了出来,需要import_records调用一下判断当前任务是否给当前线程处理,类似协程的思路。

暴露有好有坏,但已基本满足日常使用,可以往一边先放放了。读读书、看看电影,不亦乐乎 :)。


import threading

def task_pool(thread_num, task_fn):

if thread_num <= 0 :
     raise ValueError

threads = []

def gen_thread_checker(thread_id, step):

base = 1
     i = 0

def thread_checker():
         nonlocal i

i += 1
         # print((thread_id,i,step, i < base or (i - base) % step != thread_id))

if i < base or (i - base) % step != thread_id:
             return False

return True

return thread_checker

for x in range(0, thread_num):
   threads.append(threading.Thread(target=task_fn, args=(x,thread_num, gen_thread_checker(x, thread_num))))

# 启动所有线程
 for t in threads:
   t.start()
 # 主线程中等待所有子线程退出
 for t in threads:
   t.join()


import argparse
import re

import requests
from openpyxl import load_workbook
from requests import RequestException

import myThread

parser = argparse.ArgumentParser(description='美餐到店交易数据导入')
parser.add_argument('--filename', '-f', help='美餐到店交易数据 .xlsx 文件路径', required=True)
parser.add_argument('--thread_num', '-t', help='线程数量', default= 100, required=False)
parser.add_argument('--debug', '-d', help='调试模式', default= 0, required=False)
args = parser.parse_args()

filename = args.filename
thread_num = int(args.thread_num)
debug = args.debug

if debug:
   print((filename,thread_num,debug))

def add_meican_meal_record(data):
  pass

def import_records(thread_id, thread_number, thread_checker):
   wb = load_workbook(filename=filename)
   ws = wb.active

for row in ws:
       #------------------------------------------
       if row[0].value is None:
           break

if not thread_checker():
           continue
       #------------------------------------------

if row[0].value == '日期' or row[0].value == '总计' or not re.findall('^\d{4}-\d{1,2}-\d{1,2}$', row[0].value):
           continue
       else:

date = str.replace(row[0].value,'-', '')

order_id = row[3].value
           restaurant_name = row[5].value
           meal_plan_name = row[6].value
           meal_staffid = row[10].value
           identify = row[11].value

add_meican_meal_record({
               'orderId':order_id,
               'date': date,
               'meal_plan_name':meal_plan_name,
               'meal_staffid':meal_staffid,
               'identify':identify,
               'restaurant_name':restaurant_name
           })

myThread.task_pool(thread_num,import_records)

来源:https://www.tuicool.com/articles/bQZv6bm

标签:Python,多线程,处理任务
0
投稿

猜你喜欢

  • python装饰器与递归算法详解

    2022-11-03 17:05:09
  • 基于python分享一款地理数据可视化神器keplergl

    2023-09-27 20:54:05
  • FSO中的SubFolders 属性介绍

    2008-01-05 13:57:00
  • MySQL配置文件my.cnf中文版

    2011-09-30 11:06:15
  • sqlserver中去除字符串中连续的分隔符的sql语句

    2012-06-06 20:07:39
  • js插入flash可防止虚线框激活

    2009-03-13 13:31:00
  • Python基础之循环语句用法示例【for、while循环】

    2022-06-03 19:37:50
  • Python+腾讯云服务器实现每日自动健康打卡

    2023-08-18 00:22:44
  • Python响应对象text属性乱码解决方案

    2023-07-31 13:06:41
  • 用pip给python安装matplotlib库的详细教程

    2021-02-02 00:02:45
  • Python jieba 中文分词与词频统计的操作

    2022-08-31 17:08:14
  • asp的command对象的使用

    2008-06-23 13:03:00
  • 如何提高Request集合的使用效率?

    2010-06-07 20:53:00
  • 在pandas多重索引multiIndex中选定指定索引的行方法

    2023-07-07 22:04:16
  • ASP实例:使用ASP生成图片彩色校验码

    2009-01-20 16:27:00
  • Oracle RMAN快速入门指南

    2010-07-18 12:50:00
  • Python跳出多重循环的方法示例

    2022-12-18 16:28:26
  • Search File Contents PHP 搜索目录文本内容的代码

    2023-11-24 08:09:40
  • 详解Go语言中关于包导入必学的 8 个知识点

    2023-07-09 05:38:11
  • 制作Python数字华容道的实现(可选择关卡)

    2022-12-20 19:32:18
  • asp之家 网络编程 m.aspxhome.com