Python 多線程處理任務(wù)實(shí)例
美餐每天發(fā)一個(gè)用Excel匯總的就餐數(shù)據(jù),我們把它導(dǎo)入到數(shù)據(jù)庫后,行政辦公服務(wù)用它和公司內(nèi)的就餐數(shù)據(jù)進(jìn)行比對查重。
初始實(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python內(nèi)存監(jiān)控工具memory_profiler和guppy的用法詳解
這篇文章主要介紹了python內(nèi)存監(jiān)控工具memory_profiler和guppy的用法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
Python mutiprocessing多線程池pool操作示例
這篇文章主要介紹了Python mutiprocessing多線程池pool操作,結(jié)合實(shí)例形式分析了Python多線程模塊multiprocessing進(jìn)程池相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
Python如何把多個(gè)PDF文件合并代碼實(shí)例
這篇文章主要介紹了Python如何把多個(gè)PDF文件合并,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Python實(shí)現(xiàn)PC屏幕截圖并自動(dòng)發(fā)送郵箱
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)一個(gè)屏幕截圖應(yīng)用程序,可以定時(shí)截取屏幕,并將截圖通過電子郵件發(fā)送給指定的收件人,需要的可以參考下2024-12-12
擴(kuò)展Django admin的list_filter()可使用范圍方法
今天小編就為大家分享一篇擴(kuò)展Django admin的list_filter()可使用范圍方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
詳解Python Matplotlib解決繪圖X軸值不按數(shù)組排序問題
這篇文章主要介紹了詳解Python Matplotlib解決繪圖X軸值不按數(shù)組排序問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

