修改 Simpy 商店 _do_get

Modifying Simpy Store _do_get

我想通过更改 _do_get 中返回的对象来修改 simpy Store get 功能:

class mod_Store(Store):
    def _do_get(self, event):
        super()._do_get(event)
        event.value.tagged = True

env = Environment()
s = mod_Store(env)

class thing:
    pass

def putter():
    while True:
        yield s.put(thing())
        yield env.timeout(5)


def getter():
    while True:
        t = yield s.get()
        yield env.timeout(3)

env.process(putter())
env.process(getter())

env.run(until=20)

基本上,在将对象返回到 getter 之前,我想添加一个属性(标记)。但是此代码会产生以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-50-27e09b52b41a> in getter()
     19     while True:
---> 20         t = yield s.get()
     21         yield env.timeout(3)

C:\ProgramData\Anaconda3\lib\site-packages\simpy\resources\base.py in __init__(self, resource)
     77         self.callbacks.append(resource._trigger_put)
---> 78         resource._trigger_get(None)
     79 

C:\ProgramData\Anaconda3\lib\site-packages\simpy\resources\base.py in _trigger_get(self, put_event)
    223             get_event = self.get_queue[idx]
--> 224             proceed = self._do_get(get_event)
    225             if not get_event.triggered:

<ipython-input-50-27e09b52b41a> in _do_get(self, event)
      3         super()._do_get(event)
----> 4         event.value.tagged = True
      5 

C:\ProgramData\Anaconda3\lib\site-packages\simpy\events.py in value(self)
    132         if self._value is PENDING:
--> 133             raise AttributeError('Value of %s is not yet available' % self)
    134         return self._value

AttributeError: Value of <StoreGet() object at 0x9030c88> is not yet available

The above exception was the direct cause of the following exception:

AttributeError                            Traceback (most recent call last)
<ipython-input-50-27e09b52b41a> in <module>()
     24 env.process(getter())
     25 
---> 26 env.run(until=20)

C:\ProgramData\Anaconda3\lib\site-packages\simpy\core.py in run(self, until)
    135         try:
    136             while True:
--> 137                 self.step()
    138         except StopSimulation as exc:
    139             return exc.args[0]  # == until.value

C:\ProgramData\Anaconda3\lib\site-packages\simpy\core.py in step(self)
    227             exc = type(event._value)(*event._value.args)
    228             exc.__cause__ = event._value
--> 229             raise exc

AttributeError: Value of <StoreGet() object at 0x9030c88> is not yet available

查看base.py,我看到在Get的init函数中调用了_trigger_get,所以失败是有道理的,但是如何我能完成我想达到的目标吗?我的一种解决方法是在 try/catch 中进行属性赋值并捕获 AttributeErrors,但这感觉就像是 hack。

此外,如果我在 _do_get 的开头添加打印语句,它实际上会打印两次,然后引发异常,我觉得这很奇怪。

也许您可以将 StoreGet 事件子类化并在其 succeed() 方法被调用时设置属性。你还需要子类 Store 像这样:

class TaggedStoreGet(StoreGet):
    def succeed(self, value=None):
        self.tagged = True
        return super().succeed(value)


class TaggedStore(Store):
    get = BoundClass(TaggedStoreGet)

还没有测试过,但我认为它可能有效。