def __init__(self, simId): # # We create a new asyncio event loop per instance # # This way we can utilize multithreading correctly which would else be prone to collisions on the responses # # note: collosion = 2 responses at the same time, making an overlap on the first by the second and crashing JSON parsing # self.loop = asyncio.new_event_loop() # asyncio.set_event_loop(self.loop) self.simId = simId self.actor_id = "%s-%s-%s" % ("roadwork", self.simId, str(uuid.uuid4().hex)[:8]) self.proxy = ActorProxy.create(self.simId, ActorId(self.actor_id), RoadworkActorInterface)
async def main(): # Create proxy client proxy = ActorProxy.create('RoadworkActor', ActorId('1'), RoadworkActorInterface) # ----------------------------------------------- # Actor invocation demo # ----------------------------------------------- # non-remoting actor invocation print("call actor method via proxy.invoke()", flush=True) rtn_bytes = await proxy.invoke("GetMyData") print(rtn_bytes, flush=True) # RPC style using python duck-typing print("call actor method using rpc style", flush=True) rtn_obj = await proxy.GetMyData() print(rtn_obj, flush=True) # ----------------------------------------------- # Actor sim environment demo # ----------------------------------------------- await proxy.SimCreate({'env_id': 'CartPole-v1'}) await proxy.SimMonitorStart() for episode in range(2): await proxy.SimReset() step = 0 total_reward = 0 while True: step += 1 # await proxy.SimRender() # for i in range(5): action = await proxy.SimActionSample() print(f'Action taking: {action}', flush=True) obs, reward, done, info = await proxy.SimStep({'action': action}) print(f'Received:', flush=True) print(f'- Obs: {obs}', flush=True) print(f'- Rewards: {reward}', flush=True) print(f'- Done: {done}', flush=True) print(f'- Info: {info}', flush=True) total_reward += reward if done: print("Episode: {0},\tSteps: {1},\tscore: {2}".format( episode, step, total_reward)) break await proxy.SimMonitorStop()
async def main(): # Create proxy client proxy = ActorProxy.create('DemoActor', ActorId('1'), DemoActorInterface) # ----------------------------------------------- # Actor invocation demo # ----------------------------------------------- # non-remoting actor invocation print("call actor method via proxy.invoke_method()", flush=True) rtn_bytes = await proxy.invoke_method("GetMyData") print(rtn_bytes, flush=True) # RPC style using python duck-typing print("call actor method using rpc style", flush=True) rtn_obj = await proxy.GetMyData() print(rtn_obj, flush=True) # ----------------------------------------------- # Actor state management demo # ----------------------------------------------- # Invoke SetMyData actor method to save the state print("call SetMyData actor method to save the state", flush=True) await proxy.SetMyData({'data': 'new_data'}) # Invoke GetMyData actor method to get the state print("call GetMyData actor method to get the state", flush=True) rtn_obj = await proxy.GetMyData() print(rtn_obj, flush=True) # ----------------------------------------------- # Actor reminder demo # ----------------------------------------------- # Invoke SetReminder actor method to set actor reminder print("Register reminder", flush=True) await proxy.SetReminder(True) # ----------------------------------------------- # Actor timer demo # ----------------------------------------------- # Invoke SetTimer to set actor timer print("Register timer", flush=True) await proxy.SetTimer(True) # Wait for 30 seconds to see reminder and timer is triggered print("waiting for 30 seconds", flush=True) await asyncio.sleep(30) # Stop reminder and timer print("stop reminder", flush=True) await proxy.SetReminder(False) print("stop timer", flush=True) await proxy.SetTimer(False)
def car_to_json(actorType: str, actorId: str): print('/carToJSON/{0}/{1}'.format(actorType, actorId), flush=True) proxy = ActorProxy.create(actorType, ActorId(actorId), CarActorInterface) car = request.get_json() result = asyncio.run(proxy.CarToJSONAsync(car)) return result, 200
def car_from_json(actorType: str, actorId: str): print('/carFromJSON/{0}/{1}'.format(actorType, actorId), flush=True) proxy = ActorProxy.create(actorType, ActorId(actorId), CarActorInterface) body = request.get_data().decode('utf-8') result = asyncio.run(proxy.CarFromJSONAsync(body)) return jsonify(result), 200
def increment_and_get(actorType: str, actorId: str): print('/incrementAndGet/{0}/{1}'.format(actorType, actorId), flush=True) proxy = ActorProxy.create(actorType, ActorId(actorId), CarActorInterface) result = asyncio.run(proxy.IncrementAndGetAsync(1)) return jsonify(result), 200