我將以下 Python 腳本放在一起,它使用多執行緒來執行回傳字典的函式(我的實際應用程式用于加載和決議 - 但在這里將其簡化為字串操作以使其更易于顯示)。
我發現讓多執行緒在Windows中作業的唯一方法是if "__main__" == __name__:在執行之前使用。但是,這似乎會產生一個問題,即實際函式之后的任何內容都會重復多次,即使它在函式或腳本執行的一部分之外。
如何更新腳本以免得到這種遞回性?(我希望函式只回傳一次字典)。我做錯了什么?
這是我重新調整用途的腳本:
import concurrent.futures
from itertools import product
from time import process_time
# This function generates a dictionary with the string as key and a list of its letters as the value
def genDict (in_value):
out_dict = {}
out_dict[in_value] = list(in_value)
return(out_dict)
# Generate a list of all combinations of three alphabet letter strings
# this is not necesarily a best example for multithreading, but makes the point
# an io example would really accelerate under multithreading
alphabets = ['a', 'b', 'c', 'd', 'e']
listToProcess = [''.join(i) for i in product(alphabets, repeat = 4)]
print('Lenght of List to Process:', len(listToProcess))
# Send the list which is sent to the genDict function multithreaded
t1_start = process_time()
dictResult = {}
if "__main__" == __name__:
with concurrent.futures.ProcessPoolExecutor(4) as executor:
futures = [executor.submit(genDict, elem) for elem in listToProcess]
for future in futures:
dictResult.update(future.result())
t1_stop = process_time()
print('Multithreaded Completion time =', t1_stop-t1_start, 'sec.')
print('\nThis print statement is outside the loop and function but still gets wrapped in')
print('This is the size of the dictionary: ', len(dictResult))
這是我得到的輸出(請注意,時間計算以及最后的列印陳述句被“執行”多次)。輸出:
PS >> & C://multithread_test.py
Lenght of List to Process: 625
Lenght of List to Process: 625
Lenght of List to Process: 625
Multithreaded Completion time = 0.0 sec.
Multithreaded Completion time = 0.0 sec.
This print statement is outside the loop and function but still gets wrapped in
This print statement is outside the loop and function but still gets wrapped in
This is the size of the dictionary: 0
This is the size of the dictionary: 0
Lenght of List to Process: 625
Multithreaded Completion time = 0.0 sec.
This print statement is outside the loop and function but still gets wrapped in
This is the size of the dictionary: 0
Lenght of List to Process: 625
Multithreaded Completion time = 0.0 sec.
This print statement is outside the loop and function but still gets wrapped in
This is the size of the dictionary: 0
Multithreaded Completion time = 0.140625 sec.
This print statement is outside the loop and function but still gets wrapped in
This is the size of the dictionary: 625
PS >>
uj5u.com熱心網友回復:
唯一需要注意的if __name__是全域輸入的設定和要執行的函式。而已。請記住,對于多處理,每個新執行緒都會啟動一個全新的解釋器,它會重新運行您的檔案,但__name__設定為不同的值。守衛之外的任何事情都將在每個行程中再次執行。
這是組織這種代碼的方法。這行得通。
import concurrent.futures
from itertools import product
from time import process_time
# This function generates a dictionary with the string as key and a list of its letters as the value
def genDict (in_value):
out_dict = {}
out_dict[in_value] = list(in_value)
return(out_dict)
def main():
# Generate a list of all combinations of three alphabet letter strings
# this is not necesarily a best example for multithreading, but makes the point
# an io example would really accelerate under multithreading
alphabets = ['a', 'b', 'c', 'd', 'e']
listToProcess = [''.join(i) for i in product(alphabets, repeat = 4)]
print('Lenght of List to Process:', len(listToProcess))
# Send the list which is sent to the genDict function multithreaded
t1_start = process_time()
dictResult = {}
with concurrent.futures.ProcessPoolExecutor(4) as executor:
futures = [executor.submit(genDict, elem) for elem in listToProcess]
for future in futures:
dictResult.update(future.result())
t1_stop = process_time()
print('Multithreaded Completion time =', t1_stop-t1_start, 'sec.')
print('\nThis print statement is outside the loop and function but still gets wrapped in')
print('This is the size of the dictionary: ', len(dictResult))
if "__main__" == __name__:
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/496625.html
