时间不流动,我无法运行过程

时间:2017-05-19 13:16:00

标签: python generator simulation yield simpy

我正在尝试使用simpy和python 3.4在Windows上构建一个集群模拟器来模拟作业的长度。一切正常,直到模拟作业长度,似乎我的功能没有运行,模拟时间总是为0.我该如何解决这个问题?

import simpy
from random import randint

class Node:
   """
   Cluster node, runs job
   """
    def __init__(self, env):
        self.env = env
        # generate a random nodeID
        self.nodeID = randint(0, 4)

    def run(self):
        print("Into run")
        print("Doing stuff at time: ", env.now)
        yield self.env.timeout(5)
        print("Done stuff at time: ", env.now)

class Cluster:
    """
    Cluster abstracion, runs jobs on scheduled node
    """
    def __init__(self, env):
        print("Cluster created")
        self.env = env
        self.node_list = {}

    def add_node(self, node):
        print("Adding node", node.nodeID)
        self.node_list.update({node.nodeID: node})
        print("node list: ", self.node_list)

    def run_job(self, nodeID):
        print("time before process: ", self.env)
        print("running on node: ", nodeID)
        nd = self.node_list.get(str(nodeID))
        print("node to run is ", nd)
        env.process(nd.run())
        print("time after process: ", self.env)

class Scheduler:
    """
    Schedules jobs on a cluster based on some algorithm
    """

    def __init__(self, cluster, env):
        print("Scheduler created")
        self.clus = cluster
        self.env = env

    def sched(self):
        print("Scheduling..")
        for x in range(randint(0, 4)):
            print("node :", x)
            self.clus.run_job(x)

if __name__ == "__main__":
    env = simpy.Environment()
    clus = Cluster(env)
    print("START TIME = ", env.now)
    for i in range(5):
        clus.add_node(Node(env))
    sched = Scheduler(clus, env)
    sched.sched()
    print("END TIME = ", env.now)
    env.run()

0 个答案:

没有答案