[simpy] 02. Interrupt를 포함한 예제 만들기 - 비행기 주유 프로세스

2024. 5. 7. 15:07정보공유/python

  • 이번 포스팅에서 만들어볼 예제문제는 Airplane과 관련된 문제입니다.

예제1.

  • 우리는 Airplane 주유 process를 시뮬레이션해보려고 합니다.
  • 앞선 포스팅에서 만들었던 Car 예제처럼 Airplane이 Trip과 Charging을 반복합니다. 다만, 여기서는 Charging이 끝나고 나면 새로운 Airplane이 주유소로 들어옵니다.
  • 앞선 예제와 다른 점은, 중간에 어떤 이유로 Interrupt가 발생하여 process가 중단됩니다.
    • Trip duration = 2
    • Charging duration = 5
    • Intterupt at time 3
  • 이번에는, 앞선 예제와 다르게 Airplane이라는 class를 만들어서 동작시켜 보겠습니다.

1. Airplane이 동작하는 함수 만들기 

  •  airplane class안에 run함수를 만들어야 합니다.
  • airplane이 여러개가 순차적으로 올 것이기 때문에, airplane_index라는 변수가 하나씩 늘어나면서 
    • trip을 시작하고, trip duration동안 timeout했다가
    • parking spot에 도착하고, charging을 바로 하는 event를 찍어주는 것을 먼저 만들어보겠습니다.
import simpy

class Airplane():
	def __init__(self, env, trip_duration, charging_duration):
    	self.env = env
        self.trip_duration = trip_duration
        self.charging_duration = charging_duration
        self.action = env.process(self.run())
        
    def run(self):
    	airplane_index = 0
        while True:
        	airplane_index += 1
            print("Airplane{0} start trip at".format(airplane_index), self.env.now)
            yield self.env.timeout(self.trip_duration)
            print"Airplane{0} arrive in parking spot at".format(airplane_index), self.env.now)
            print"Airplane{0} starts chaging".format(airplane_index), self.env.now)
            yield self.env.process(self.charge(airplane_index))
            #charge 함수 동작하도록
            
	def charge(self, airplane_index):
    	yield self.env.timeout(charging_duration)
        print("Airplane{0} finishes charging".format(airplane_index), self.env.now)

 

  • 이게 기본적인 format입니다.
  • 이제 우리는 try & except를 이용해서 중간에 interrupt가 들어오는 상황을 가정하겠습니다.
    • 이 때는 "simpy.Interrupt"를 사용합니다.
    • interrupt 함수를 따로 정의해줘야합니다. (airplane class안에 만들어도 됩니다)
    • airplane.action.interrupt로 동작시킵니다.
import simpy

class Airplane():
	def __init__(self, env, trip_duration, charging_duration):
    	self.env = env
        self.trip_duration = trip_duration
        self.charging_duration = charging_duration
        self.action = env.process(self.run())
        
    def run(self):
    	airplane_index = 0
        while True:
        	airplane_index += 1
            print("Airplane{0} start trip at".format(airplane_index), self.env.now)
            yield self.env.timeout(self.trip_duration)
            print"Airplane{0} arrive in parking spot at".format(airplane_index), self.env.now)
            print"Airplane{0} starts chaging".format(airplane_index), self.env.now)
            try:
            	yield self.env.process(self.charge(airplane_index))
                #원래 하려고 했던 코드는 try안에 넣고
            except simpy.Interrupt:
            	print("Interrupt at", self.env.now)
            
	def charge(self, airplane_index):
    	yield self.env.timeout(charging_duration)
        print("Airplane{0} finishes charging".format(airplane_index), self.env.now)
        
        
def interrupt(env, airplane):
	yield env.timeout(3)
    airplane.action.intterupt()

 

정상적으로 동작했을 경우 아래와 같은 결과가 나와야 합니다

 

블로그에서 직접 코드를 입력하다보니 오타가 있을 수 있습니다^^;