失败时发送消息(Python)

时间:2018-04-18 06:43:57

标签: python-3.x messaging

我试图模拟两台机器工作,并在随机时间失败。当他们失败时,他们会给予援助这两台机器是不同机器的更大系统的一部分,需要知道其邻居何时未能完成其工作。

到目前为止,我已经对这两台机器进行了模拟,但我无法弄清楚如何在没有每台机器需要知道整个系统的情况下向邻居发送消息?

这是我到目前为止所做的:

import simpy
import random



random_seed=42
MTTF = 3500
break_mean = 1 / MTTF
sim_time = 4 * 7*24*60 # 4 weeks 24/7
num_machines = 2
rep_time = 30
tpp = 20 #20 minutes to make each part
neighbour = 3 #How many should it send to?


#Creating a class called messaging which is an object.
class messaging(object):
#DEfing the initilizing function, and initilize self, Environment, and       capacity which is set to infinity, by a simpy core-function.
    def __init__(self, env, capacity=simpy.core.Infinity):
        self.env = env
        self.capacity = capacity
        self.pipes = []

#Making a function which work on everything that is a part of the message. With name Put.
    def put(self, value):

        if not self.pipes: #How to get this error?
            raise  runtime_error('There are no output pipes.')
        #Create a variable, events, store to it pipe values
        events = broken_machine()
        return self.env.all_of(events)

    def get_output_conn(self):
        #Set the capacity of pipe variable to store infinity.
        pipe = simpy.Store(self.env, capacity=self.capacity)
        #to each pipes, add(or append) pipe
        self.pipes.append(pipe)
        return pipe

def mesg_generator(number, env, out_pipe):
    msg = ('Failed')

def message_reciever(name, env, in_pipe):
    while True:
        msg = yield in_pipe.get()

        print("%s received message: %s" % (number, msg[1]))

def time_per_part():
    return tpp

def ttf():
    return random.expovariate(break_mean)


class Machine(object):

    def __init__(self, env, number, repair):
    #self.arg = arg
        self.env = env
        self.number = number
        self.parts_made = 0
        self.times_broken = 0
        self.broken = False

        self.process = env.process(self.working(repair))
        env.process(self.broken_machine())


def working(self, repair):
    while True:
        work = time_per_part()
        while work:
            try:
                begin = self.env.now
                yield self.env.timeout(work)
                work = 0
            except simpy.Interrupt:
                self.broken = True
                work -= self.env.now - begin

                with repair.request(priority = 1) as req:
                    yield req
                    yield self.env.timeout(rep_time)
                self.times_broken +=1
                yield message_reciever()
                #print('Machine down')
                self.broken = False #Machine fixed again

        self.parts_made +=1

def broken_machine(self):
    while True:
        yield self.env.timeout(ttf())
        if not self.broken:
            self.process.interrupt()

def other_jobs(env, repair):
    while True:
        work = tpp
        while work:
            with repair.request(priority=2) as req:
                yield req
                try:
                    begin = env.now
                    yield env.timeout(work)
                    work = 0
                 except simpy.Interrupt:
                    work -= env.now - begin


print("This simulates machines 3 and 4 doing the same tasks.")

random.seed(random_seed)

env = simpy.Environment()
pipe = simpy.Store(env)
bc_pipe = messaging(env)
repair = simpy.PreemptiveResource(env, capacity = 1)
machines = [Machine(env, 'Machine %d' % i, repair)
            for i in range(num_machines)]
env.process(other_jobs(env, repair))
env.run(until=sim_time)

#Show how many times each machine failed:
for machine in machines:
    print("%s broke down %d times" %(machine.number, machine.times_broken))

0 个答案:

没有答案