シミュレーションに python と simpy を使用しています。シミュレーションでは、1 つのインスタンス (中断) が別のインスタンス (中断) によって中断される可能性があります。割り込みごとにネストされた try except ステートメントを使用します。ネストされた try except ステートメントは、中断の最大回数がわかっている場合に機能します。
問題は、中断が何回発生するかわからないことです (1、2、3、... の可能性があります)。何度も中断されたオブジェクトを処理する方法がわかりません。
以下のコードは 3 回の中断に対して機能しますが、4 回目の中断が含まれると機能しなくなります (3 つのネストされた try except ステートメントのため)。
不明な回数の割り込みを処理できるように、コードをより汎用的にすることは可能ですか?
どんな助けでも大歓迎です。
コード:
import simpy
import random
class Interupted(object):
def __init__(self, env):
self.env = env
self.isInterrupted = False
self.action = env.process(self.run())
def run(self):
self.isInterrupted = False
try:
print('uninterrupted at %s' % (self.env.now))
yield self.env.timeout(3)
except simpy.Interrupt as interrupt:
print(interrupt.cause)
try:
self.isInterrupted = True
print('interrupted at %s' % (self.env.now))
yield self.env.timeout(10)
except simpy.Interrupt as interrupt:
print(interrupt.cause)
try:
self.isInterrupted = True
print('interrupted at %s' % (self.env.now))
yield self.env.timeout(10)
except simpy.Interrupt as interrupt:
print(interrupt.cause)
self.isInterrupted = True
print('interrupted at %s' % (self.env.now))
yield self.env.timeout(10)
class Interruptor(object):
def __init__(self, env, interrupted):
self.env = env
self.interrupted = interrupted
self.action = env.process(self.run(interrupted))
def run(self, interrupted):
yield self.env.timeout(1)
interrupted.action.interrupt("first interrupt")
yield self.env.timeout(1)
interrupted.action.interrupt("second interrupt")
yield self.env.timeout(1)
interrupted.action.interrupt("third interrupt")
yield self.env.timeout(1)
interrupted.action.interrupt("fourth interrupt")
env = simpy.Environment()
interrupted = Interupted(env)
interruptor = Interruptor(env, interrupted)
env.run(until=15)
出力:
uninterrupted at 0
first interrupt
interrupted at 1
second interrupt
interrupted at 2
third interrupt
interrupted at 3
Traceback (most recent call last):
File "interrupt.py", line 58, in <module>
env.run(until=15)
File "/usr/local/lib/python2.7/dist-packages/simpy/core.py", line 137, in run
self.step()
File "/usr/local/lib/python2.7/dist-packages/simpy/core.py", line 229, in step
raise exc
simpy.events.Interrupt: Interrupt('fourth interrupt')
使用したバージョン:
- パイソン: 2.7.3
- シンピー: 3.0.7