回调函数在处理超时事件时不会激活

问题描述 投票:0回答:1

我想为多个资源构建一个具有多个超时的连续流程,之后应在其特定的流程时间点立即触发各个进程,新的超时。

对于我的每个资源,我都产生了单独的超时事件,订单簿中有随机延迟时间。对于每个计时器,我都实现了一个回调函数,它将资源标记为空,并且应该激活 processActivation-Function,该函数首先用于产生空资源的超时。似乎没有一个回调函数被激活,我不知道是什么问题。我也想知道他们是否会工作,是否会在每次超时结束后立即在各自的时间点开始。我非常感谢帮助,因为我似乎无法为我的问题找到有效的解决方案:)

代码的特定部分在图像上:

enter image description here

代码的顶部: enter image description here

python events callback simpy
1个回答
0
投票
#Import Libraries
import random
import simpy
import numpy
from simpy.events import AnyOf, AllOf, Event

#Setting of Parameters
nOrders = 3
nOperations = 4
nVariants = 4
nMachinesPerOperation = [1, 2, 2]

#Initialization of Variables
env = simpy.Environment()

Orderbook = [[[0 for _ in range(3)] for _ in range(nOperations+1)] for _       in range(nOrders)]
for a in range(nOrders):
    Orderbook[a][0][0] = a+1  
    Orderbook[a][0][1] = random.randint(0, nVariants)  
    Orderbook[a][0][2] = 1 
    for b in range(1, nOperations+1):
        Orderbook[a][b][0] = b-1
        Orderbook[a][b][1] = random.randint(10, 40)  
        Orderbook[a][b][2] = 1  

Resources = []
for b in range(len(nMachinesPerOperation)):
    Resources.append([simpy.Resource(env, capacity=1) for c in range(nMachinesPerOperation[b])])

ResourcesStatus= []
for k in range(len(nMachinesPerOperation)):
    ResourcesStatus.append([0 for l in range(nMachinesPerOperation[k])])

TimeoutEvents= []

#Functions
def ResourceFree(ResourcesStatus,nMachinesPerOperation):
    for i in range(len(nMachinesPerOperation)):
            for j in range(nMachinesPerOperation[i]):
                if ResourcesStatus[i][j] == 0:
                    return True
    return False
                
def onTimeoutComplete(i, j, TimeoutEvents, ResourcesStatus, Orderbook, env, Resources, nMachinesPerOperation):
    ResourcesStatus[i][j] = 0
    ProcessActivation(env, Orderbook, Resources, nMachinesPerOperation, ResourcesStatus,TimeoutEvents)
                        
def yieldTimeoutEvents(action,i,j,TimeoutEvents,ResourcesStatus,Orderbook,env,Resources,nMachinesPerOperation):
    TimeoutEvents.append(simpy.events.Timeout(env,Orderbook[action][j+1][1]))
    ResourcesStatus[i][j]= 1
                            
    yield TimeoutEvents[len(TimeoutEvents) - 1]
    TimeoutEvents[len(TimeoutEvents) - 1].callbacks.append(onTimeoutComplete(i, j, TimeoutEvents, ResourcesStatus, Orderbook, env, Resources, nMachinesPerOperation))
            
def ProcessActivation(env,Orderbook,Resources,nMachinesPerOperation,ResourcesStatu s,TimeoutEvents): 
    
    if ResourceFree(ResourcesStatus,nMachinesPerOperation):
            for i in range(len(nMachinesPerOperation)):
                        for j in range(nMachinesPerOperation[i]):
                                action = numpy.random.randint(3)
                                if not ResourcesStatus[i][j]: 
                                    Orderbook[action][j+1][2] = 0
                                    env.process(yieldTimeoutEvents(action,i,j,TimeoutEvents,ResourcesStatus,Orderbook,env,Resources,nMachinesPerOperation))
                                    
    else:                          
            yield env.timeout(1)                    
    
#Start Simulation        
    env.process(ProcessActivation(env,Orderbook,Resources,nMachinesPerOperation,ResourcesStatus,TimeoutEvents))
env.run(until=100)

                
© www.soinside.com 2019 - 2024. All rights reserved.