시작하는 예는 많이있다. Pool.map
또는 Pool.starmap
을 사용하여 필요한 것을 정확히 수행 할 수 있습니다. 예 :
from multiprocessing import Pool
class Adder:
"""I'm using this class in place of a monte carlo simulator"""
def add(self, a, b):
return a + b
def setup(x, y, z):
"""Sets up the worker processes of the pool.
Here, x, y, and z would be your global settings. They are only included
as an example of how to pass args to setup. In this program they would
be "some arg", "another" and 2
"""
global adder
adder = Adder()
def job(a, b):
"""wrapper function to start the job in the child process"""
return adder.add(a, b)
if __name__ == "__main__":
args = list(zip(range(10), range(10, 20)))
# args == [(0, 10), (1, 11), ..., (8, 18), (9, 19)]
with Pool(initializer=setup, initargs=["some arg", "another", 2]) as pool:
# runs jobs in parallel and returns when all are complete
results = pool.starmap(job, args)
print(results) # prints [10, 12, ..., 26, 28]
일부 답변을 검색하면 많은 샘플 코드가 생성됩니다. 당신이 당신의 일부를 제공하지 않으면, 우리는 당신을 도울 수 없을 것입니다. Btw, 스레드를 잠 그려면 [semaphores] (https://docs.python.org/2/library/threading.html#semaphore-objects)를 사용하십시오. – Raskayu
[예제] (https : //docs.python .org/3/library/multiprocessing.html # 공유 상태 - 프로세스 간) 공식 문서에 있습니까? – mata
시뮬레이션이 진행되는 동안 어레이에 액세스해야합니까? 그렇지 않다면'Pool.map' 함수를 사용할 수 있습니다. – Dunes