我正在尝试使用多处理程序包实现以下代码-
from multiprocessing import Process from manna_1D import Lattice,info
import numpy as np import time
L = 2**3 N = 2**4 propagation_time = 3 cores = 1
lattice = Lattice()
if __name__ == '__main__':
#info('main line')
lattice.set_length(L)
# initialize lattice
print("initialization process")
for p in range(cores):
print("started %s process" % int(p+1))
p = Process(target=lattice.add_particles, args=(N,))
p.start()
p.join()
print(lattice.sites)
我也有一个Lattice课-
import os #for processing analysis
def info(title):
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
import numpy as np
class Lattice:
sites = []
length = 0
def __init__(self):
self.sites = [0]*self.length
def set_length(self,length): #setting a length initalizes all the sites values to zero
self.length=length
self.sites = [0]*self.length
def add_particles(self,n):
if (self.length > 0):
print(info("add %s particles" % n))
for p in range(n):
loc = np.random.randint(self.length-1)
self.sites[loc]+=1
print(loc,self.sites[loc])
else:
print("cannot add particles on a lattice without sites")
问题是,当我运行main.py时,我得到以下输出-
initialization process
started 1 process
add 16 particles
('module name:', 'manna_1D')
('parent process:', 17850)
('process id:', 29319)
None
(2, 1)
(3, 1)
(0, 1)
(1, 1)
(2, 2)
(6, 1)
(6, 2)
(6, 3)
(0, 2)
(6, 4)
(4, 1)
(5, 1)
(4, 2)
(5, 2)
(4, 3)
(4, 4)
[0, 0, 0, 0, 0, 0, 0, 0]
在元组中显示在左侧添加了粒子的位置,而在右侧显示了当前在该位置的粒子数量。最后,打印每个位置的粒子数量输出。它显示所有站点都是空的。
我认为这是对Process对象的某种滥用,但可以肯定地说。帮助和见解将不胜感激!