【发布时间】:2021-12-22 17:00:18
【问题描述】:
我在一个对象中使用 multiprocessing.Pool 类并尝试以下操作:
from multiprocessing import Pool, Lock
class myobject:
def __init__(self):
self.Lock = Lock()
self.file = open('someiterablefile')
def function(self):
self.lock.acquire()
g = getNext(self.file)
self.lock.release()
return g
def anotherfunction(self):
pool = Pool()
results = pool.map(self.function, range(10000))
pool.close()
pool.join()
return results
但是,我收到一个运行时错误,指出锁对象只能通过继承在进程之间共享。我对python和多线程相当陌生。我怎样才能走上正轨?
【问题讨论】:
-
始终将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
-
也许你应该将
self.Lock发送到self.function作为第二个参数。 -
您在函数定义中忘记了
self。 -def function(self),def anotherfunction(self)
标签: python multiprocessing pool