复制包含SimPy进程的类实例的最简单方法是什么?

复制包含SimPy进程的类实例的最简单方法是什么?

问题描述:

我正在尝试创建一个我可以模拟的类实例的副本,而不会影响该类的原始实例.我尝试使用copy.copy,但是遇到了这个问题:

I'm trying to create a copy of a class instance that I can simulate without affecting the original instance of the class. I've tried using copy.copy, but I run into this problem:

system.simulate(until=100)

print(system.env.now) # prints 100

copy_of_system = copy.copy(system)
copy_of_system.simulate(until=200)

print(copy_of_system.env.now) # prints 200
print(system.env.now) # prints 200, but should print 100

当我使用copy.deepcopy时,我得到TypeError: can't pickle generator objects.有没有有效的方法来创建system对象的独立副本?

When I use copy.deepcopy I get TypeError: can't pickle generator objects. Is there any effective way to create an independent copy of the system object?

设置现有环境simpy.Environment采取几种不同的并行执行路径的一种方法是在完成环境设置后使用os.fork() .然后,您可以例如利用multiprocessing中的进程间通信原语(例如Queues等)来收集不同模拟的有趣结果.这需要编写大量与手动分叉相关的样板,但这可能是值得的.

One way of having an existing simpy.Environment take several different paths of execution in parallel would be to use os.fork() when you're done setting up the Environment. You then can, for example, leverage the interprocess communication primitives in multiprocessing, such as Queues, etc, for collecting the interesting results of the different simulations. This requires writing a fair amount of boilerplate associated with manual forking, but it can be more than worth it.

注意:据我所知,os.fork()仅在类似Unix的操作系统下可用.

NB: os.fork(), that I know of, is available only under Unix-like OSes.