Python Simpy进程-无法使用生成器发送方法? 背景
SimPy进程据说是文档中的一个生成器,我希望它具有中的send方法Python Simpy进程-无法使用生成器发送方法? 背景,python,generator,simpy,Python,Generator,Simpy,SimPy进程据说是文档中的一个生成器,我希望它具有中的send方法 流程由简单的Python描述。您可以调用它们process function或process method,这取决于它是类的普通函数还是方法。在它们的生命周期中,它们创建事件并产生事件,以便等待它们被触发 我可以使用send从另一台发电机向发电机发送消息,例如将充电站发电机注入电动汽车发电机 from typing import Dict, Optional, Generator import time def ev(
from typing import Dict, Optional, Generator
import time
def ev(env) -> Generator:
while True:
next_charging_station = yield
print("next_charging_station[{}]".format(next_charging_station))
def dispathcer(env, ev) -> Generator:
while True:
time.sleep(3)
print('Dispatching the next charging station')
ev.send("new station")
process_ev = ev(env)
next(process_ev)
process_dispatcher = dispathcer(env, process_ev)
但是,SymPy进程没有发送方法
import simpy
env = simpy.Environment()
def ev(env):
while True:
next_charging_station = yield
print("next_charging_station[{}]".format(next_charging_station))
def dispathcer(env, ev):
while True:
print('Dispatching the next charging station at %d' % (env.now))
ev.send("new station")
rocess_ev = env.process(ev(env))
process_dispatcher = env.process(dispathcer(env, process_ev))
-----
Dispatching the next charging station at 0
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-ece25f162510> in <module>
1 process_ev = env.process(ev(env))
----> 2 process_dispatcher = env.process(dispathcer(env, process_ev))
<ipython-input-11-b89aeb1a9073> in dispathcer(env, ev)
11 while True:
12 print('Dispatching the next charging station at %d' % (env.now))
---> 13 ev.send("hoge")
AttributeError: 'Process' object has no attribute 'send'
import simpy
env = simpy.Environment()
def ev(env):
while True:
next_charging_station = yield
print("next_charging_station[{}]".format(next_charging_station))
def dispathcer(env, ev):
while True:
print('Dispatching the next charging station at %d' % (env.now))
ev.send("new station")
rocess_ev = env.process(ev(env))
process_dispatcher = env.process(dispathcer(env, process_ev))
-----
Dispatching the next charging station at 0
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-ece25f162510> in <module>
1 process_ev = env.process(ev(env))
----> 2 process_dispatcher = env.process(dispathcer(env, process_ev))
<ipython-input-11-b89aeb1a9073> in dispathcer(env, ev)
11 while True:
12 print('Dispatching the next charging station at %d' % (env.now))
---> 13 ev.send("hoge")
AttributeError: 'Process' object has no attribute 'send'
import simpy
env = simpy.Environment()
def charging_station(env):
print('Start charging at %d' % (env.now))
charge_duration = 3
yield env.timeout(charge_duration)
return
def ev(env):
while True:
print('Start driving at %d' % (env.now))
driving_duration = 5
yield env.timeout(driving_duration)
print('Stop at a charging station at %d' % (env.now))
# [Q] Instead of creating a charging process inside the ev process,
# how to get it injected?
yield env.process(charging_station(env)) # <-----
env.process(ev(env))
env.run(until=20)
-----
Start driving at 0
Stop at a charging station at 5
Start charging at 5
Start driving at 8
Stop at a charging station at 13
Start charging at 13
Start driving at 16
def new_ev(env): # <---- how to implement?
while True:
next_charging_station = yield # Get the next station process
print('Start charging at %d' % (env.now))
yield next_charging_station
print('Start driving at %d' % (env.now))
driving_duration = 5
yield env.timeout(driving_duration)
import random
import simpy
env = simpy.Environment()
class Dispatcher(object):
env = None
def __init__(self, env):
self.env = env
print('Start dispatcher at time %d' % self.env.now)
# Start the run process everytime an instance is created.
#self.process = env.process(self.run())
def run(self):
while True:
print('Start dispatcher at time %d' % self.env.now)
yield self.env.timeout(1)
def next_charger_station(self):
return self._charge(random.randint(1, 1000), self.env)
def _charge(self, id, env):
print('station %s start charging at time %d' % (id, env.now))
yield env.timeout(5)
return
def ev(env, dispatcher):
while True:
print('EV stops at the new charging station at time %d' % (env.now))
yield env.process(dispatcher.next_charger_station())
print('EV start driving from the charging station at time %d' % (env.now))
yield env.timeout(5)
dispatcher = Dispatcher(env)
process_ev = env.process(ev(env, dispatcher))
env.run(until=30)