python操作excel表格写入多行和多列_python多处理:写入同一excel-fi
1) Why did you implement time.sleep in several places in your 2nd method?
在__main__,time.sleep(0.1),給啟動的process一個啟動的時間片。
在f2(fq, q)中,給queue一個時間片,以將所有緩沖數(shù)據(jù)刷新到管道,并
使用q.get_nowait()。
在w(q)中,僅用于測試writer.to_excel(...)的長期運行,
我把這個拿走了。在2) What is the difference between pool.map and pool = [mp.Process( . )]?
使用pool.map不需要Queue,不傳遞任何參數(shù),更短的代碼。
worker_process必須立即返回result并終止。
pool.map只要iteration都完成了,就會啟動一個新進程。
在此之后必須處理results。在
使用pool = [mp.Process( . )],啟動nprocesses。
process終止于queue.EmptyCan you think of a situation where you would prefer one method over the other?
方法1:快速設(shè)置,序列化,只對結(jié)果感興趣繼續(xù)。
方法二:如果你想把所有的工作負荷都并行進行。在
你不能在進程中使用global writer。
writer實例必須屬于一個process。在
mp.Pool的用法,例如:def f1(k):
# *** DO SOME STUFF HERE***
results = pd.DataFrame(df_)
return results
if __name__ == '__main__':
pool = mp.Pool()
results = pool.map(f1, range(len(list_of_days)))
writer = pd.ExcelWriter('../test/myfile.xlsx', engine='xlsxwriter')
for k, result in enumerate(results):
result.to_excel(writer, sheet_name=list_of_days[k])
writer.save()
pool.close()
這導(dǎo)致.to_excel(...)在__main__進程中按順序調(diào)用。在
如果你想要并行.to_excel(...),你必須使用mp.Queue()。
例如:
worker過程:
^{pr2}$
writer過程:def w(q):
writer = pd.ExcelWriter('myfile.xlsx', engine='xlsxwriter')
while True:
try:
titel, result = q.get()
except ValueError:
writer.save()
exit(0)
result.to_excel(writer, sheet_name=titel)
__main__過程:if __name__ == '__main__':
w_q = mp.Queue()
w_p = mp.Process(target=w, args=(w_q,))
w_p.start()
time.sleep(0.1)
f_q = mp.Queue()
for i in range(len(list_of_days)):
f_q.put(i)
pool = [mp.Process(target=f2, args=(f_q, w_q,)) for p in range(os.cpu_count()+1)]
for p in pool:
p.start()
time.sleep(0.1)
for p in pool:
p.join()
w_q.put('STOP')
w_p.join()
測試Python:3.4.2-熊貓:0.19.2-十lsxwriter:0.9.6
總結(jié)
以上是生活随笔為你收集整理的python操作excel表格写入多行和多列_python多处理:写入同一excel-fi的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT 1048 数字加密
- 下一篇: python绘图turtle小猪_tur