访问已在其他类中创建的变量。蟒蛇

时间:2017-11-18 11:14:35

标签: python function class

我不熟悉python类。我需要将变量服务时间(位于类模拟中,位于函数服务下)传递给函数customer,更具体地说是传递给priority = servicetime的行。当然,两个服务时间必须相等,并且也等于类的特定实例。怎么办呢?

import simpy
import numpy as np
import random
import itertools

global meanqtime # average queue time
global meanstime # average server time
meanqtime=[]
meanstime=[]

class simulation(object):
    """


    """

    def __init__(self,env,num_machines,servicerate):
        self.env=env
        self.machine=simpy.PriorityResource(env,num_machines)
        self.servicerate=servicerate



    def service (self,customer):


        servicetime=random.expovariate(servicerate) #this 
        yield self.env.timeout(servicetime)


def  customer(env, name, server):
    """The customer process arrives at the server
       and requests to be serviced

    """    
    artime=env.now
    #print('%s arrives at the desk at %.2f.' % (name, env.now))
    with server.machine.request(priority=servicetime) as request: #to be here
        yield request

        #print('%s starts servicing at %.2f.' % (name, env.now))
        qtime=env.now
        meanqtime.append(qtime-artime)
        yield env.process(server.service(name))
        #print('%s leaves the desk at %.2f.' % (name, env.now))
        stime=env.now
        meanstime.append(stime-artime)

def setup(env, num_servers, servicerate, arrivalrate):

    desk = simulation(env, num_servers, servicerate)

    while  True:

        for i in itertools.count():
            #t=np.random.poisson(arrivalrate)
            t=random.expovariate(arrivalrate)
            yield env.timeout(t)
            env.process(customer(env, 'Person %d' % i, desk))
            #yield env.timeout(t)


num_servers = 1  #number of servers
arrivalrate=0.1 #per hour
servicerate=0.125#hours
T=100

env = simpy.Environment()
env.process(setup(env, num_servers, servicerate, arrivalrate))

# Execute!
env.run(until=T) 

0 个答案:

没有答案