일부 코드를 테스트하고 있습니다 (더 빠르게하려고하지만 차이점을 이해하려고 시도하고 있습니다). 메모리에 테이블을 만드는 루프가 있습니다. 그런 다음 다중 프로세스를 시도했지만 다중 프로세스에서 메모리 사용량이 이상하게 보입니다. 테이블을 자체적으로 실행하면 시스템의 모든 메모리가 필요할 때까지 테이블이 계속 커지고 성장하지만 멀티 프로세싱을 사용하면 전체 시간이 낮게 유지되어 어떤 일을하는지 궁금해집니다. 다중 처리되지 않은 코드를 빠르게 재 작성하려고합니다. 여기 다중 처리에서 공유 항목에 메모리 제한이 있습니까?
은 (단지/추가는 시스템 프로세스를 볼 빠르거나 느린 실행하기 위해 데이터 변수에서 항목을 제거 Multiprocessed 상단에 있고 nonmulti 하단에 있습니다.) 일부 코드입니다 :from multiprocessing import Pool
from multiprocessing.managers import BaseManager, DictProxy
from collections import defaultdict
class MyManager(BaseManager):
pass
MyManager.register('defaultdict', defaultdict, DictProxy)
def test(i,x, T):
target_sum = 1000
# T[x, i] is True if 'x' can be solved
# by a linear combination of data[:i+1]
#T = defaultdict(bool) # all values are False by default
T[0, 0] = True # base case
for s in range(target_sum + 1): #set the range of one higher than sum to include sum itself
#print s
for c in range(s/x + 1):
if T[s - c * x, i]:
T[s, i + 1] = True
data = [2,5,8,10,12,50]
pool = Pool(processes=2)
mgr = MyManager()
mgr.start()
T = mgr.defaultdict(bool)
T[0, 0] = True
for i, x in enumerate(data): # i is index, x is data[i]
pool.apply_async(test, (i,x, T))
pool.close()
pool.join()
pool.terminate()
print 'size of Table(with multiprocesing) is:', len(T)
count_of_true = []
for x in T.items():
if T[x] == True:
count_of_true.append(x)
print 'total number of true(with multiprocesing) is ', len(count_of_true)
#now lets try without multiprocessing
target_sum = 100
# T[x, i] is True if 'x' can be solved
# by a linear combination of data[:i+1]
T1 = defaultdict(bool) # all values are False by default
T1[0, 0] = True # base case
for i, x in enumerate(data): # i is index, x is data[i]
for s in range(target_sum + 1): #set the range of one higher than sum to include sum itself
for c in range(s/x + 1):
if T1[s - c * x, i]:
T1[s, i + 1] = True
print 'size of Table(without multiprocesing) is ', len(T1)
count = []
for x in T1:
if T1[x] == True:
count.append(x)
print 'total number of true(without multiprocessing) is ', len(count)
실험으로 두 코드를 두 파일에 넣고 나란히 실행했습니다. 두 개의 멀티는 약 20 %를 차지하며 각각 0.5 %의 메모리 만 사용합니다. 단일 프로세스 (멀티 없음)는 코어의 75 %와 최대 50 %의 메모리 사용량을 사용합니다.
당신은 다음과 같이 씁니다 : "스스로 실행하면 ..."Pool (프로세스 = 1)을 설정하는 것에 대해 이야기합니까? – itsafire
정확하지 않습니다. 위의 코드에서 하나는 다중 프로세스 풀에 래핑되고 다른 하나는 풀없이 실행됩니다. – Lostsoul