欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 多線程處理任務(wù)實(shí)例

 更新時(shí)間:2021年11月06日 14:38:06   作者:soul11201  
這篇文章主要介紹了Python 多線程處理任務(wù),下面文章利用一個(gè)真實(shí)實(shí)例圍繞Python 多線程處理任務(wù)的相關(guān)資料展開內(nèi)容,需要的朋友可以參考一下

美餐每天發(fā)一個(gè)用Excel匯總的就餐數(shù)據(jù),我們把它導(dǎo)入到數(shù)據(jù)庫后,行政辦公服務(wù)用它和公司內(nèi)的就餐數(shù)據(jù)進(jìn)行比對(duì)查重。

初始實(shí)現(xiàn)是單線程,和import_records去掉多線程后的部分差不多。

讀取Excel數(shù)據(jù) —> 發(fā)送到行政服務(wù)接口

安全起見線上操作放在了晚上進(jìn)行。運(yùn)行時(shí)發(fā)現(xiàn)每條數(shù)據(jù)導(dǎo)入消耗1s多,晚上十點(diǎn)開始跑這幾千條數(shù)據(jù)想想都讓人崩潰。

等著也是干等,下樓轉(zhuǎn)兩圈透透氣,屋里齷齪的空氣讓人昏昏沉沉,寒冷讓人清醒不少,突然想到為什么不用多線程呢?

第一版多線程和處理業(yè)務(wù)的程序糅合在了一起,跟屎一樣難讀。后面兩天又抽了點(diǎn)時(shí)間重構(gòu)了幾個(gè)版本,分離出來一個(gè)線程池、迭代器和import_records。

清晰不少,但是迭代器被暴露了出來,需要import_records調(diào)用一下判斷當(dāng)前任務(wù)是否給當(dāng)前線程處理,類似協(xié)程的思路。

暴露有好有壞,但已基本滿足日常使用,可以往一邊先放放了。讀讀書、看看電影,不亦樂乎 :)。

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

  # 啟動(dòng)所有線程
  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='美餐到店交易數(shù)據(jù)導(dǎo)入')
parser.add_argument('--filename', '-f', help='美餐到店交易數(shù)據(jù) .xlsx 文件路徑', required=True)
parser.add_argument('--thread_num', '-t', help='線程數(shù)量', default= 100, required=False)
parser.add_argument('--debug', '-d', help='調(diào)試模式', 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 == '總計(jì)' 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)

到此這篇關(guān)于Python 多線程處理任務(wù)實(shí)例的文章就介紹到這了,更多相關(guān)Python 多線程處理任務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論