SimPy > process interaction

KyungOok,Sung
1 min readJun 29, 2021

The Process instance that is returned by Environment.process() can be utilized for process interactions. The two most common examples for this are to wait for another process to finish and to interrupt another process while it is waiting for an event.

Environment.process ()에서 반환하는 Process 인스턴스는 프로세스 상호 작용에 사용할 수 있습니다. 이에 대한 가장 일반적인 두 가지 예는 다른 프로세스가 완료 될 때까지 대기하고 이벤트를 기다리는 동안 다른 프로세스를 중단하는 것입니다.

class Car(object):
def __init__(self, env):
self.env = env
# Start the run process everytime an instance is created.
self.action = env.process(self.run())

def run(self):
while True:
print('Start parking and charging at %d' % self.env.now)
charge_duration = 5
# We yield the process that process() returns
# to wait for it to finish
try:
yield self.env.process(self.charge(charge_duration))
except simpy.Interrupt:
# When we received an interrupt, we stop charging and
# switch to the "driving" state
print('Was interrupted. Hope, the battery is full enough ...')
def charge(self, duration):
yield self.env.timeout(duration)
def driver(env, car):
yield env.timeout(3)
car.action.interrupt() # interrupt
import simpy
env = simpy.Environment()
car = Car(env)
env.process(driver(env, car))
env.run(until=15)
[out]
Start parking and charging at 0
Was interrupted. Hope, the battery is full enough ...
Start parking and charging at 3
Start parking and charging at 8
Start parking and charging at 13

■ 참고한 사이트
https://simpy.readthedocs.io/en/latest/simpy_intro/process_interaction.html
https://greatjoy.tistory.com/m/74

--

--