def get_pactor_ref(self): """Get a reference to a pykka actor for this.""" if not self.is_active(): raise NotRunningError("This game is not active.") else: actor_urn = cache.get(str(self.uuid)) actor_ref = ActorRegistry.get_by_urn(actor_urn) return actor_ref
def is_active(self): actor_urn = cache.get(str(self.uuid)) if actor_urn: if ActorRegistry.get_by_urn(actor_urn): return True else: cache.delete(str(self.uuid)) return False else: return False
def on_receive(self, msg): if msg['type'] == 'config': # Set the sixteenth note period for this Actor self.period = 1.0/msg['bpm']*60.0 / 4.0 # Get NoteActor URN self.target = ActorRegistry.get_by_urn(msg['target']) self.tick(0) elif msg['type'] == 'play': self.playing = True; self.tick(0) elif msg['type'] == 'stop': self.playing = False
def test_register_get_by_filter(actor_class): actor_ref = actor_class().start() # 已经完成注册啦了. # step1 测试 urn查询 assert ActorRegistry.get_by_urn(actor_ref.actor_urn) == actor_ref # step2 测试类名查询 assert ActorRegistry.get_by_class(actor_ref.actor_class)[0] == actor_ref # # step3 测试类名字符查询 assert ActorRegistry.get_by_class_name( 'CustomThreadingActor')[0] == actor_ref ActorRegistry.stop_all()
def test_registration(self): f1 = FunctionalUnit((0, 0)) f2 = FunctionalUnit((0, 1)) f3 = FunctionalUnit((0, 2)) a1 = OperatorActor.start(f1) time.sleep(.1) a2 = OperatorActor.start(f2) time.sleep(.1) a3 = OperatorActor.start(f3) time.sleep(.1) registry = ActorRegistry() for ref in [registry.get_by_urn(ref.actor_urn) for ref in [a1, a2, a3]]: a = ref._actor
def on_receive(self, msg): if msg['type'] == 'config': self.gui_target = ActorRegistry.get_by_urn(msg['gui_target']) elif msg['type'] == 'seq-config': # Reset the Euclidean rhythm at seq_num using the parameters k and n self.seq[msg['seq_num']] = {'i': msg['seq_num'], 'r': euclidean_rhythm(msg['k'], msg['n']), 'n': self.seq[msg['seq_num']]['n']} elif msg['type'] == 'seq-mute': self.mutes[msg['seq_num']] = not self.mutes[msg['seq_num']] self.gui_target.tell({'type': 'show-mute', 'seq_num': msg['seq_num'], 'muted': self.mutes[msg['seq_num']]}) elif msg['type'] == 'tick': map(self.send, self.seq)
def reply(self, message, text, opts=None): """Reply to the sender of the message with a containing the text :param message: the message to reply to :param text: the text to reply with :param opts: A dictionary of additional values to add to metadata :return: None """ metadata = Metadata(source=self.actor_urn, dest=message['metadata']['source']).__dict__ metadata['opts'] = opts message = Message(text=text, metadata=metadata, should_log=message['should_log']).__dict__ dest_actor = ActorRegistry.get_by_urn(message['metadata']['dest']) if dest_actor is not None: dest_actor.tell(message) else: raise("Tried to send message to nonexistent actor")
def reply(self, message, text, opts=None): """ Reply to the sender of the provided message with a message \ containing the provided text. :param message: the message to reply to :param text: the text to reply with :param opts: A dictionary of additional values to add to metadata :return: None """ metadata = Metadata(source=self.actor_urn, dest=message['metadata']['source']).__dict__ metadata['opts'] = opts message = Message(text=text, metadata=metadata, should_log=message['should_log']).__dict__ dest_actor = ActorRegistry.get_by_urn(message['metadata']['dest']) if dest_actor is not None: dest_actor.tell(message) else: raise("Tried to send message to nonexistent actor")
def iap(pid): g = Greeter.start() g.actor_urn = pid ActorRegistry.unregister(g) ActorRegistry.register(g) #greeter2 = Greeter.start(1).proxy() #greeter3 = Greeter.start(2).proxy() #greeter4 = Greeter.start(3).proxy() #greeter5 = Greeter.start(4).proxy() #a = ActorRegistry.get_all() #for i in a: # ppid = i.proxy().pid.get() # print (ppid, type(ppid)) # if ppid == '123': # print('haha, got you') # else: # print(ppid) for i in ActorRegistry.get_all(): print ('here') print(i.proxy().actor_urn.get(), type(i.proxy().actor_urn.get())) if i.proxy().actor_urn.get() == pid: print ('haha, got you') print ('=============') a = ActorRegistry.get_by_urn(pid) print (a) print ('=============') for i in ActorRegistry.get_all(): print(i) print (i.__dict__) print ('here') print(i.proxy().actor_urn.get(), type(i.proxy().actor_urn.get())) if i.proxy().actor_urn.get() == 'asdf': print ('haha, got you') #future = greeter.pprint({'message':'%s' % pid}) return 'as'
def on_start(self): self.on_start_was_called.set() if ActorRegistry.get_by_urn(self.actor_urn) is not None: self.actor_was_registered_before_on_start_was_called.set()
def _iap(pid): player = get_player(pid) print (ActorRegistry.get_by_urn(pid)) future = player.proxy().pprint({'message':'%s' % pid}) return future.get()
def get_player(pid): player = ActorRegistry.get_by_urn(pid) if player is None: player = Player.start(pid) return player
def test_actors_may_be_looked_up_by_urn(actor_ref): result = ActorRegistry.get_by_urn(actor_ref.actor_urn) assert result == actor_ref
def test_get_by_urn_returns_none_if_not_found(): result = ActorRegistry.get_by_urn('urn:foo:bar') assert result is None
def test_actors_may_be_looked_up_by_urn(self): result = ActorRegistry.get_by_urn(self.a_actor_0_urn) self.assertEqual(self.a_actors[0], result)
def on_failure(self, exception_type, exception_value, traceback): ref = ActorRegistry.get_by_urn(self.actor_urn) logger.exception('Lego crashed: ' + str(ref)) logger.exception(exception_type) logger.exception(exception_value)
def ask_to(self, to: str, mes): self.events.append(Event(type='ask', message=mes)) res = ActorRegistry.get_by_urn(to).ask(mes) self.events.append(Event(type='ask res', message=res)) return res
def get_player(pid): player = ActorRegistry.get_by_urn(pid) if player is None: player = Player.start(pid) return player
def test_get_by_urn_returns_none_if_not_found(): result = ActorRegistry.get_by_urn('urn:foo:bar') assert result is None
def test_get_by_urn_returns_none_if_not_found(self): result = ActorRegistry.get_by_urn('urn:foo:bar') self.assertEqual(None, result)
def __init__(self, root, timing_act_urn, note_act_urn): # Make this thread a daemon so it ends when the main thread exits super(GuiActor, self).__init__(use_daemon_thread=True) self.widgets = [] # Set up transport controls timing_actor = ActorRegistry.get_by_urn(timing_act_urn) def make_transport_cb(msg): return lambda: timing_actor.tell({'type': msg}) frame = tk.Frame(root, padx=5) play_b = tk.Button(frame, width=5, text='Play') stop_b = tk.Button(frame, width=5, text='Stop') play_b.config(command=make_transport_cb('play')) stop_b.config(command=make_transport_cb('stop')) play_b.grid(row=0, column=0, padx=10, pady=10) stop_b.grid(row=1, column=0, padx=10, pady=10) frame .grid(row=0, column=0) # Set up tk GUI # Create each sequencer frame note_actor = ActorRegistry.get_by_urn(note_act_urn) seq_names = ['Kick', 'Snare', 'Cl. HiHat', 'Op. HiHat', 'Clave', 'Cowbell'] for idx in range(6): frame = tk.Frame(root, borderwidth=1, padx=5, relief=tk.RIDGE) seq_label = tk.Label (frame, text=seq_names[idx]) k_label = tk.Label (frame, text='k:') k_entry = tk.Entry (frame, width=5) n_label = tk.Label (frame, text='n:') n_entry = tk.Entry (frame, width=5) start_b = tk.Button(frame, width=7, text='Start') mute_b = tk.Button(frame, width=7, text='Mute') # Capture the current sequence index, sequence k Entry object, # and sequence n Entry object with a closure in order to keep # references to them in the callback for this start button def make_start_cb(seq_idx, seq_k, seq_n): # Send a message to the NoteActor with the info # from this sequence's config frame return lambda: note_actor.tell({'type': 'seq-config', 'seq_num': seq_idx, 'k': int(seq_k.get()), 'n': int(seq_n.get())}) start_b.config(command=make_start_cb(idx, k_entry, n_entry)) # Capture the current sequence index, with a closure to use them # in a callback for the mute button def make_mute_cb(seq_idx): return lambda: note_actor.tell({'type': 'seq-mute', 'seq_num': seq_idx}) mute_b.config(command=make_mute_cb(idx)) seq_label.grid(row=0, column=0, columnspan=2) k_label .grid(row=1, column=0, padx=5) n_label .grid(row=2, column=0, padx=5) k_entry .grid(row=1, column=1) n_entry .grid(row=2, column=1) start_b .grid(row=3, column=0, pady=5, columnspan=2) mute_b .grid(row=4, column=0, pady=5, columnspan=2) frame.grid(row=0, column=idx+1) self.widgets.append([frame, seq_label, k_label, n_label])
def on_start(self): self.on_start_was_called.set() if ActorRegistry.get_by_urn(self.actor_urn) is not None: self.actor_was_registered_before_on_start_was_called.set()
def test_get_by_urn_returns_none_if_not_found(self): result = ActorRegistry.get_by_urn('urn:foo:bar') self.assertEqual(None, result)
def test_actors_may_be_looked_up_by_urn(self): result = ActorRegistry.get_by_urn(self.a_actor_0_urn) self.assertEqual(self.a_actors[0], result)
def tell_to(self, to: str, mes): self.events.append(Event(type='tell', message=mes)) ActorRegistry.get_by_urn(to).tell(mes)
def on_failure(self, exception_type, exception_value, traceback): ref = ActorRegistry.get_by_urn(self.actor_urn) logger.exception('Lego crashed: ' + str(ref)) logger.exception(exception_type) logger.exception(exception_value)
def test_actors_may_be_looked_up_by_urn(actor_ref): result = ActorRegistry.get_by_urn(actor_ref.actor_urn) assert result == actor_ref