【发布时间】:2022-01-18 17:34:30
【问题描述】:
当我尝试执行这段代码时:
import pathos
from network import *
def simulation(error_rate=1):
network = Network(error_rate=1)
network.gen_transceiver_pair(1)
network.run()
mend = network.master_transceivers[0].cycle[0, 2]
send = network.slave_transceivers[0].cycle[0, 2]
return send if send > mend else mend
if __name__ == '__main__':
p = pathos.pools.ParallelPool(nodes=10)
result = p.map(simulation, [1, 1, 1])
我收到此错误:
An error has occured during the function import
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\network_simu\venv\lib\site-packages\ppft\__main__.py", line 100, in run
six.exec_(__fobj)
File "<string>", line 1, in <module>
File "<string>", line 6, in Network
NameError: name 'signal' is not defined
A fatal error has occured during the function execution
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\network_simu\venv\lib\site-packages\ppft\__main__.py", line 109, in run
__f = locals()[ppc.str_(__fname)]
KeyError: 'simulation'
是因为我在函数内部生成了一个Network 对象吗?理想情况下,我希望以不同的错误率(1、.9 等)运行模拟 10^6-10^7 次。我尝试使用multiprocessing 运行,但无法腌制该功能,因此我尝试使用Pathos。
编辑:我正在使用来自blinker 模块的signal。代码在正常执行时工作。我已经完成了 10^6 次迭代,没有任何问题,但这需要很长时间。
编辑2: 我正在使用这样的信号:
class Pool:
def __init__(self):
Transceiver.pool_add.connect(self.add)
Transceiver.pool_remove.connect(self.remove)
Network.return_cycle.connect(self.set_cycle)
self.cycle = 0
self.pool = []
def set_cycle(self, sender):
self.cycle = sender.cycle
def add(self, sender):
if VERBATIM:
print("added " + sender.t_id)
self.pool.insert(0, sender)
sender.cycle[0, 0] = self.cycle
def remove(self, sender):
if VERBATIM:
print("removed " + sender.t_id)
self.pool.remove(sender)
sender.cycle[0, 2] = self.cycle
class Transceiver(NetworkComponent):
__STATUS = ["off", "t_on", "r_on", "on"]
pool_add = signal("pool_add")
pool_remove = signal("pool_remove")
__ID = iter(range(1, 41))
Edit3:所以我删除了signal 的使用。现在我得到这个错误:
Backend TkAgg is interactive backend. Turning interactive mode on.
An error has occured during the function import
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\network_simu\venv\lib\site-packages\ppft\__main__.py", line 100, in run
six.exec_(__fobj)
File "<string>", line 1, in <module>
File "<string>", line 7, in Network
NameError: name 'np' is not defined
A fatal error has occured during the function execution
np 是 numpy... 很奇怪
【问题讨论】:
-
blinker 不保证密钥“信号”的可用性。您可以在不崩溃的情况下处理此异常吗?
-
我不确定我是否理解。所以通常由于进程不共享相同的内存空间,发出信号不应该“触发”其他类实例?信号被定义为类属性。编辑:添加了我如何实现信号的示例。
-
我去掉了signal的使用,现在得到一个numpy没有定义的错误?
-
既然你没有
import numpy as np,我对它没有定义并不感到惊讶。 -
Numpy 被 Network 对象使用。此外,
from network import *和network已将 numpy 作为 np 导入...还尝试在我的主脚本中将 numpy 作为 np 导入,但这也不起作用。我可以在没有悲伤的情况下进行模拟。
标签: python-3.x multiprocessing python-multiprocessing pathos