
下列代码中,func 函数无法被 pool.apply_async 调用,这是什么情况?
import time from multiprocessing.pool import Pool class Test: def __init__(self): self.pool = Pool(5) def func(self): time.sleep(0.2) print("1") def run(self): for i in range(10): self.pool.apply_async(self.func) # 这里的 func 为什么不能进入执行? time.sleep(3) self.pool.close() self.pool.join() if __name__ == '__main__': t = Test() t.run() 1 Rob007 2018-05-08 22:45:27 +08:00 |
2 justou 2018-05-08 22:48:44 +08:00 你把 func 改成 staticmethod 试试; 再试试改成这样报什么错: import time from multiprocessing.pool import Pool class Test: def __init__(self): self.pool = Pool(5) def func(self): time.sleep(0.2) print("1") return 1 def run(self): results = [self.pool.apply_async(self.func) for _ in range(10)] for res in results: print(res.get()) time.sleep(3) self.pool.close() self.pool.join() if __name__ == '__main__': t = Test() t.ru() 再搜索下可能就很多收获了:) |