Python 多線程處理任務(wù)實(shí)例
美餐每天發(fā)一個(gè)用Excel匯總的就餐數(shù)據(jù),我們把它導(dǎo)入到數(shù)據(jù)庫(kù)后,行政辦公服務(wù)用它和公司內(nèi)的就餐數(shù)據(jù)進(jìn)行比對(duì)查重。
初始實(shí)現(xiàn)是單線程,和import_records去掉多線程后的部分差不多。
讀取Excel數(shù)據(jù) —> 發(fā)送到行政服務(wù)接口
安全起見(jiàn)線上操作放在了晚上進(jìn)行。運(yùn)行時(shí)發(fā)現(xiàn)每條數(shù)據(jù)導(dǎo)入消耗1s多,晚上十點(diǎn)開(kāi)始跑這幾千條數(shù)據(jù)想想都讓人崩潰。
等著也是干等,下樓轉(zhuǎn)兩圈透透氣,屋里齷齪的空氣讓人昏昏沉沉,寒冷讓人清醒不少,突然想到為什么不用多線程呢?
第一版多線程和處理業(yè)務(wù)的程序糅合在了一起,跟屎一樣難讀。后面兩天又抽了點(diǎn)時(shí)間重構(gòu)了幾個(gè)版本,分離出來(lái)一個(gè)線程池、迭代器和import_records。
清晰不少,但是迭代器被暴露了出來(lái),需要import_records調(diào)用一下判斷當(dāng)前任務(wù)是否給當(dāng)前線程處理,類(lèi)似協(xié)程的思路。
暴露有好有壞,但已基本滿(mǎn)足日常使用,可以往一邊先放放了。讀讀書(shū)、看看電影,不亦樂(lè)乎 :)。
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)文章
Python多線程編程(二):?jiǎn)?dòng)線程的兩種方法
這篇文章主要介紹了Python多線程編程(一):?jiǎn)?dòng)線程的兩種方法,本文講解了將函數(shù)傳遞進(jìn)Thread對(duì)象、繼承自threading.Thread類(lèi)兩種方法,需要的朋友可以參考下2015-04-04
Pygame實(shí)現(xiàn)小球躲避實(shí)例代碼
大家好,本篇文章主要講的是Pygame實(shí)現(xiàn)小球躲避實(shí)例代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽2021-12-12
詳解python 條件語(yǔ)句和while循環(huán)的實(shí)例代碼
這篇文章主要介紹了詳解python 條件語(yǔ)句和while循環(huán),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
pytorch查看網(wǎng)絡(luò)參數(shù)顯存占用量等操作
這篇文章主要介紹了pytorch查看網(wǎng)絡(luò)參數(shù)顯存占用量等操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05
解決PIP安裝第三方庫(kù)報(bào)錯(cuò)SSL: CERTIFICATE_VERIFY_FAILED問(wèn)題
這篇文章主要介紹了解決PIP安裝第三方庫(kù)報(bào)錯(cuò)SSL: CERTIFICATE_VERIFY_FAILED問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Python發(fā)送郵件實(shí)現(xiàn)基礎(chǔ)解析
這篇文章主要介紹了Python發(fā)送郵件實(shí)現(xiàn)基礎(chǔ)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Python數(shù)據(jù)結(jié)構(gòu)與算法之圖的基本實(shí)現(xiàn)及迭代器實(shí)例詳解
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之圖的基本實(shí)現(xiàn)及迭代器,結(jié)合實(shí)例形式詳細(xì)分析了數(shù)據(jù)結(jié)構(gòu)與算法中圖的實(shí)現(xiàn)及迭代器相關(guān)算法原理與操作技巧,需要的朋友可以參考下2017-12-12
使用pytorch實(shí)現(xiàn)可視化中間層的結(jié)果
今天小編就為大家分享一篇使用pytorch實(shí)現(xiàn)可視化中間層的結(jié)果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12

