例如,通过类方法使用线程或多处理
class ExampleClass:
def run(self, run_param):
print run_param
def run_parallel(self, num):
processes = []
for i in xrange(num):
p = multiprocessing.Process(target=self.run, args=(i,))
# should I pass self to args like
# p = multiprocessing.Process(target=self.run, args=(self, i))
processes.append(p)
for p in processes:
p.start()
for p in processes:
p.join()
答案 0 :(得分:1)
由于您将self.run
作为绑定方法进行传递,因此无需将self
作为第一个参数传递给它:
p = multiprocessing.Process(target=self.run, args=(i,))
self
仅在您将未绑定方法作为Process
参数传递给target
时才需要:
p = multiprocessing.Process(target=ExampleClass.run, args=(self, i))