复制包含SimPy进程的类实例的最简单方法是什么? [英] What is the easiest way to copy a class instance that contains SimPy processes?

查看:106
本文介绍了复制包含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.

这篇关于复制包含SimPy进程的类实例的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆