2015-04-30 28 views
1

我目前使用SimPy來模擬和模擬服務器進程,我希望此進程根據接收此消息的位置執行不同的操作。如何讓一個進程等待多個資源?

SimPy文檔顯示如何等待多個事件: 例如:yield event1 |事件2

但是,我目前正試圖等待資源從多個商店變爲可用。

場景如下: 服務器S正在等待可能來自各種渠道的消息。這些通道中的每個通道都可能具有不同的功能,這些功能會影響消息到達它的時間。

這裏是有問題的代碼:

resources = [inchannel.get() for inchannel in inchannels] 
msg = yield simpy.events.AnyOf(env, resources) 

其中隨路是存儲該輸入的各種信道進行建模到服務器的陣列。

我遇到的問題是,它似乎只接受來自其中一個通道的消息,無論它首先接收哪一個通道。收到第一條消息後,它接受來自該通道的消息並忽略其他消息。

我也曾嘗試以下操作:

resource = inchannel[0].get() | inchannel[1].get() | ... 
msg = yield resource 

在這種情況下,它只能從隨路接收[0]

回答

1

你必須創建在每次迭代中獲取事件的一個新的列表。如果您重新使用舊列表,它仍將包含第一次迭代中觸發的事件。

這應該工作:

inchannel = [simpy.Store(env) for i in range(3)] 

while True: 
    # Make a new list of Get events in each iteration 
    events = [ic.get() for ic in inchannel] 

    # Wait until (at least) one of them was triggered 
    res = yield env.any_of(events) 

    # Cancel all remaining requests, because you will make 
    # new ones in the next iteration. 
    # Do this *before* you yield anything 
    [evt.cancel() for evt in evens] 

    # Handle all messages (there *might* be more than one) 
    for msg in res.values(): 
     handle_message(msg) 
相關問題