2016-01-18 19 views
-2

我正在寫這個編碼中的數字來存檔,但這樣做的數量需要花費數小時才能完成,只需要幾個小時就可以使用一個進程。需要幫助多重處理這個編碼

from multiproccessing import Pool 

print('What number would you like to start at?') 
datastart = raw_input() #user input for datastart 

x= int(datastart) #sets x to datastart 

print('What number would you like to end at?') 
dataend= raw_input() #user input to question at hand and sets var. 

print('would you like to write file in normal or reverse? \n (n/r)') 
detailer=raw_input() #sets forward or reverse writing mode 

print('what password length would you like?') 
passwordlen=int(raw_input())# sets length of file to be writen 

print('Please wait while file is created!') 
f=open(str(datastart)+'-'+str(dataend)+'-'+str(passwordlen)+'.txt','a') 

def NumberCrunch(nc): 

    while detailer == 'n': 
     x=datastart 
     while x <= int(dataend): 
      num= str(x) 
      while len(num) < passwordlen: 
       num='0'+num 
      if len(num) == passwordlen: 
       f.write(num+'\n') 
      x+=1 


    while detailer == 'r': 
     x=int(dataend) 
     while x >= int(datastart): 
      num= str(x) 
      while len(num) < passwordlen: 
       num='0'+num 
      if len(num +'\n'): 
       f.write(num +'\n') 


      x-=1 

if __name__ == '__main__': 
    with Pool(process = 16) as p: 
     p.map(NumberCrunch,chunksize =10) 
     p.close() 
     p.join() 


f.close() 
print('File has been created! Enjoy!') 

我使用運行Python 2.7版64位,決鬥至強16個核2.20雙四核總,32GB RAM和3張特斯拉卡的服務器。修改池(process =(您的cpu核心數))以確保它在您的計算機上運行。

我正嘗試使用所有內核來處理從數據存儲到數據結束的數據範圍,在正向寫入模式或數據結束到反向寫入模式下的數據存儲。 我試過多處理隊列。仍然只使用一個進程或提出系統退出。

有沒有更好的方式來編寫多處理程序,或者我錯過了什麼。

會是更好的,而不是使用:

from multiprocessing import Pool 

print('What number would you like to start at?') 
datastart = raw_input() #user input for datastart 

x= int(datastart) #sets x to datastart 

print('What number would you like to end at?') 
dataend= raw_input() #user input to question at hand and sets var. 

print('would you like to write file in normal or reverse? \n (n/r)') 
detailer=raw_input() #sets forward or reverse writing mode 

print('what password length would you like?') 
passwordlen=int(raw_input())# sets length of file to be writen 

print('Please wait while file is created!') 
f=open(str(datastart)+'-'+str(dataend)+'-'+str(passwordlen)+'.txt','a') 

def NumberCrunch(nc): 
    while detailer == 'f': 
     for i in range(datastart,dataend,1): 
      num = str(i) 
      while len(num) < passwordlen: 
       num = '0' + num 
      if len(num)== passwordlen: 
       f.write(num+'n') 
    while detailer == 'r': 
     for i in range(dataend,datastart,-1): 
      num= str(i) 
      while len(num) < passwordlen: 
       num = '0'+num 
      if len(num)== passwordlen: 
       f.write(num+'\n') 
if __name__=='__main____': 
    with Pool(process = 16) as p: 
     p.map(Numbercrunch,i,chuncksize=10) 
     p.close() 
     p.join() 
f.close() 
p.close() 
print('File has been created') 

任何意見或幫助將極大地幫助。先進的謝謝!

+0

'while detailer =='n':' - 你什麼時候預計'detailer' * stop *是''n''? – user2357112

+0

錯別字應該是'f'。我將編輯該codiing。 –

+0

你的代碼是一個馬虎混亂的許多錯誤。你在你的頭上。購買一本開始的Python書。閱讀。繼續努力吧。 – 7stud

回答

0

當我使用多處理時,我使用Process而不是Pool。像這樣:

from multiprocessing import Process 

def main_process(myargs): 
    #do stuff here 

#main_data defined elsewhere with items inside. 
for k,myargs in main_data.items(): 
    p = Process(target=main_process, args=(myargs,)) 
    p.start() 
+0

謝謝你,我會試試這個。我還沒有嘗試過這個過程。 –

+0

更改爲流程精心打造,使用了98%的CPU處理能力,並在幾分鐘內完成了整個文件的寫入。非常感謝您的回覆。 –

+0

@Mikey如果這解決了您的問題,請點擊複選標記以表明它是答案。謝謝 – plhyhc