問(wèn)題描述
這一次我面臨一個(gè)設(shè)計(jì)"問(wèn)題.使用 Python,我實(shí)現(xiàn)了一個(gè)使用 5 個(gè)參數(shù)的數(shù)學(xué)算法.為了找到這 5 個(gè)參數(shù)的最佳組合,我使用 5 層嵌套循環(huán)來(lái)枚舉給定范圍內(nèi)的所有可能組合.完成所需的時(shí)間似乎超出了我的預(yù)期.所以我覺(jué)得是時(shí)候使用多線(xiàn)程了……
this time i'm facing a "design" problem. Using Python, I have a implement a mathematical algorithm which uses 5 parameters. To find the best combination of these 5 parameters, i used 5-layer nested loop to enumerate all possible combinations in a given range. The time it takes to finish appeared to be beyond my expectation. So I think it's the time to use multithreading...
嵌套循環(huán)的核心任務(wù)是計(jì)算和保存.在當(dāng)前代碼中,每個(gè)計(jì)算的結(jié)果都附加到一個(gè)列表中,并且該列表將在程序結(jié)束時(shí)寫(xiě)入一個(gè)文件.
The task in the core of nested loops are calculation and saving. In current code, result from every calculation is appended to a list and the list will be written to a file at the end of program.
由于我對(duì)任何語(yǔ)言的多線(xiàn)程都沒(méi)有太多經(jīng)驗(yàn),更不用說(shuō) Python,我想請(qǐng)教一些關(guān)于這個(gè)問(wèn)題的結(jié)構(gòu)應(yīng)該是什么的提示.即,如何將計(jì)算動(dòng)態(tài)分配給線(xiàn)程,線(xiàn)程如何保存結(jié)果,然后將所有結(jié)果合并到一個(gè)文件中.希望線(xiàn)程數(shù)可以調(diào)整.
since I don't have too much experience of multithreading in any language, not to mention Python, I would like to ask for some hints on what should the structure be for this problem. Namely, how should the calculations be assigned to the threads dynamically and how should the threads save results and later combine all results into one file. I hope the number of threads can be adjustable.
任何帶有代碼的插圖都會(huì)很有幫助.
Any illustration with code will be very helpful.
非常感謝您的寶貴時(shí)間,我很感激.
thank you very much for your time, I appreciate it.
第二天更新:感謝所有有用的答案,現(xiàn)在我知道它是多處理而不是多線(xiàn)程.我總是混淆這兩個(gè)概念,因?yàn)槲艺J(rèn)為如果它是多線(xiàn)程的,那么操作系統(tǒng)會(huì)在可用時(shí)自動(dòng)使用多個(gè)處理器來(lái)運(yùn)行它.今晚我會(huì)抽出時(shí)間來(lái)實(shí)踐一下多處理.
update of 2nd Day: thanks for all helpful answers, now I know that it is multiprocessing instead of multithreading. I always confuse with these two concepts because I think if it is multithreaded then the OS will automatically use multiple processor to run it when available. I will find time to have some hands-on with multiprocessing tonight.
推薦答案
你可以嘗試使用jug,我為非常相似的問(wèn)題編寫(xiě)的一個(gè)庫(kù).然后你的代碼看起來(lái)像
You can try using jug, a library I wrote for very similar problems. Your code would then look something like
from jug import TaskGenerator
evaluate = TaskGenerator(evaluate)
for p0 in [1,2,3]:
for p1 in xrange(10):
for p2 in xrange(10,20):
for p3 in [True, False]:
for p4 in xrange(100):
results.append(evaluate(p0,p1,p2,p3,p4))
現(xiàn)在您可以運(yùn)行任意數(shù)量的進(jìn)程(如果您可以訪(fǎng)問(wèn)計(jì)算機(jī)集群,甚至可以跨網(wǎng)絡(luò)運(yùn)行).
Now you could run as many processes as you'd like (even across a network if you have access to a computer cluster).
這篇關(guān)于使用 Python 將巨大的嵌套循環(huán)劃分為 8 個(gè)(或更多)進(jìn)程的巧妙方法是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!