人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

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

發(fā)布日期:2021-12-13 15:17 | 文章來源:站長之家

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

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

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

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

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

第一版多線程和處理業(yè)務(wù)的程序糅合在了一起,跟屎一樣難讀。后面兩天又抽了點時間重構(gòu)了幾個版本,分離出來一個線程池、迭代器和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))))
  # 啟動所有線程
  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 == '總計' 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ù)實例的文章就介紹到這了,更多相關(guān)Python 多線程處理任務(wù)內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部