1

我观察到 SimPy 事件调度的一些“奇怪”行为。

当一个 nan 值被用作env.timeout方法中的延迟时,它有时会覆盖方法until = XXX内部的参数env.run

我已经概述了一个最小的示例,以显示这种不一致的行为。如果您运行所有 3 个测试用例,您将看到测试 2 永远运行。

查看源代码,似乎是超时和调度之间的一些交互,但没有比这更进一步。

问:有人可以帮我理解,为什么until当按特定顺序在timout方法中输入空值时,参数会被覆盖?


import simpy
import numpy as np
# Set up a minimal SimPy process
def func1(env, time, name):
    while True:
        print(f'process: {name}, delayed for {time},  time:', env.now)
        # print(env._queue)
        # print('================')
        yield env.timeout(time)

def test_one():
    """Stops after 20 days - Normal process at the start"""
    env = simpy.Environment() 
    env.process(func1(env = env, time = 2, name = '3'))
    env.process(func1(env = env, time = np.nan, name = '2'))
    env.process(func1(env = env, time = np.nan, name = '1'))
    env.process(func1(env = env, time = np.nan, name = '3'))
    env.run(until = 20)
    
def test_two():
    """Runs forever - Normal process at the end"""
    env = simpy.Environment() 
    env.process(func1(env = env, time = np.nan, name = '2'))
    env.process(func1(env = env, time = np.nan, name = '1'))
    env.process(func1(env = env, time = np.nan, name = '3'))
    env.process(func1(env = env, time = 2, name = '3'))
    env.run(until = 20)
    
def test_three():
    """Stops after 20 days - Normal process in 2nd position"""
    env = simpy.Environment() 
    env.process(func1(env = env, time = np.nan, name = '2'))
    env.process(func1(env = env, time = 2, name = '3'))
    env.process(func1(env = env, time = np.nan, name = '1'))
    env.process(func1(env = env, time = np.nan, name = '3'))
    env.run(until = 20)
4

0 回答 0