concurrent.futures는 여러 프로세스 (또는 스레드)에서 실행되도록 인수를 산세에 의존한다는 것을 이해했습니다. 논쟁의 복사본을 만들지 않아야합니까? 리눅스에서는 그렇게하지 않는 것 같습니다. 즉 명시 적으로 사본을 전달해야합니다. 지구에왜 concurrent.futures가 인수 사본을 작성하지 않습니까?
from __future__ import print_function
import time
import random
try:
from concurrent import futures
except ImportError:
import futures
def work_with_rands(i, rands):
print('in function', i, rands)
def main():
random.seed(1)
rands = [random.randrange(100) for _ in range(10)]
# sequence 1 and sequence 2 should give the same results but they don't
# only difference is that one uses a copy of rands (i.e., rands.copy())
# sequence 1
with futures.ProcessPoolExecutor() as ex:
for i in range(4):
print("<{}> rands before submission: {}".format(i, rands))
ex.submit(work_with_rands, i, rands)
random.shuffle(rands)
print('-' * 30)
random.seed(1)
rands = [random.randrange(100) for _ in range(10)]
# sequence 2
print("initial sequence: ", rands)
with futures.ProcessPoolExecutor() as ex:
for i in range(4):
print("<{}> rands before submission: {}".format(i, rands))
ex.submit(work_with_rands, i, rands[:])
random.shuffle(rands)
if __name__ == "__main__":
main()
에서 오는 [97, 32, 17, 15, 57, 97, 63, 72, 60, 8]
입니다 :
<0> rands before submission: [17, 72, 97, 8, 32, 15, 63, 97, 57, 60]
<1> rands before submission: [97, 15, 97, 32, 60, 17, 57, 72, 8, 63]
<2> rands before submission: [15, 57, 63, 17, 97, 97, 8, 32, 60, 72]
<3> rands before submission: [32, 97, 63, 72, 17, 57, 97, 8, 15, 60]
in function 0 [97, 15, 97, 32, 60, 17, 57, 72, 8, 63]
in function 1 [97, 32, 17, 15, 57, 97, 63, 72, 60, 8]
in function 2 [97, 32, 17, 15, 57, 97, 63, 72, 60, 8]
in function 3 [97, 32, 17, 15, 57, 97, 63, 72, 60, 8]
여기에 코드입니다 :
나는 다음과 같은 결과를 이해하기 위해 노력하고있어? submit
에 전달 된 시퀀스 중 하나도 아닙니다.
결과는 모든 스레드와의 돌연변이에 같은 목록을 공유 파이썬 2
솔루션 (명시 적 사본)이 있습니다. 나는 그것이 (1) 피클 때 피클을 만들지 않는지, 피클 링인지 그리고 (2) 어디에서''[97, 32, 17, 15, 57, 97, 63, 72, 60, 8]''오고있다. – ariddell
shuffle에서 온다 shuffle이 실제로 완료 될 때까지 shuffle이 여러 번 목록을 변경시킨다. 나는 혼란 스러울 수도 있다고 설명하기 위해 대답을 업데이트했다. –
내 독서는 공유 된 데이터가 없다는 것이다. 절인하고 따로 보내. 그게 내 혼란의 원천이며 그 질문을 던지는 이유입니다. – ariddell