0

我正在与 simpy 一起使用容器来安排作业。我面临一个问题,即只有那些在第一次迭代时开始的工作才会继续迭代。即前 3 个工作不断迭代。不让第 4 份工作进入这个过程。其次我还想知道所有作业的总执行时间?如果有人可以帮助我,我将不胜感激。

import simpy
from simpy.core import StopSimulation
from simpy.events import Timeout


class Cluster:
        def __init__(self, env):
         self.total_machines = simpy.Container(env,init=6)
         self.end_time=0
 
#users of resource
class Job:
    #enter: time the job enters the system
    #timeout is how long the job occupies a resource for
    #resources is how many resources a job needs in order to run
    def __init__(self, env, job_id,job_arrivaltime,job_time, job_machine_req):
        self.env = env
        self.job_id = job_id  
        self.job_arrivaltime = job_arrivaltime
        self.job_time = job_time
        self.job_machine_req = job_machine_req
 

#system
def system(env, job, cluster):
    while job.job_machine_req<=cluster.total_machines.level:
        print("cuurent level of cluster",cluster.total_machines.level)
        print('%s arrives at %s' % (job.job_id, env.now)) 
        yield cluster.total_machines.get(job.job_machine_req) #get the required resources
        print('%s starts running with %s resources at %s' % (job.job_id, job.job_machine_req, env.now)) 
        yield env.timeout(job.job_time) #wait for desired time of the job
        print('%s completed job at %s' % (job.job_id, env.now)) 
        yield cluster.total_machines.put(job.job_machine_req)
        

env = simpy.Environment()  #create an simpy enviroment
cluster = Cluster(env) #intialize the cluster object

jobs = [
        Job(env, '1', 0, 3, 2),
        Job(env, '2', 0, 2, 2),
        Job(env, '3', 0, 3, 1),
        Job(env, '4', 0, 4, 3),
    ]
for job in jobs:
    env.process(system(env, job, cluster))    
env.run(until=4)     
    
4

0 回答 0