def async_propose(): self.contract_net.send_propose(reply) # Wait for response try: accept_proposal = yield reply display_message( self.aid.name, f'I received ACCEPT-PROPOSAL: {accept_proposal.content} from {accept_proposal.sender.name}' ) # I'mma do some job in another thread def job(): display_message(self.aid.name, f"I'mma do my job") time.sleep(5) inform_message = accept_proposal.create_reply() inform_message.set_content(10 * int(reply.content)) # Use reactor thread to send final results call_from_thread( lambda: self.contract_net.send_inform(inform_message)) call_in_thread(job) except FipaRejectProposalHandler as h: reject_proposal = h.message display_message( self.aid.name, f'I received REJECT-PROPOSAL: {reject_proposal.content} from {reject_proposal.sender.name}' )
def loop(): if self.breaker_tripped(): display_message(f'{self.id}@{self.ip}:{self.port}', 'Abertura de chave detectada') args = ('XCBR', ) call_in_thread(self.callback, self, *args) # Chama função novamente em 1 segundo call_later(1.0, loop)
def on_request(self, message): content = message.content display_message(self.aid.name, f'I received REQUEST: {content} from {message.sender.name}') def do_long_job(): # Massive calculations part I display_message(self.aid.name, f'Doing long job') time.sleep(1.5) question = f'{randint(1, 50)}+{randint(100, 150)}' def async_request(): request_calc = ACLMessage() request_calc.set_content(question) request_calc.add_receiver(self.calculator_aid) self.consult_behavior.send_request(request_calc) while True: try: response_calc = yield request_calc display_message( self.aid.name, f'I received INFORM: {response_calc.content} from {response_calc.sender.name}' ) except FipaProtocolComplete: break def more_long_job(): # Massive calculations part II display_message(self.aid.name, f'Doing second long job') time.sleep(1.25) return response_calc.content def callback(result): # There is still a reference to the incoming request display_message(self.aid.name, f'Calling callback') def return_inform(): reply_inform = message.create_reply() reply_inform.set_content(f'The result is: {result}') self.inform_behavior.send_inform(reply_inform) # Use reactor thread to send message call_from_thread(return_inform) # Another blocking method in other thread, this time using callback defer_to_thread(more_long_job, callback) # Async method using reactor, must be called from reactor thread call_from_thread(self.consult_behavior.run, async_request()) # Blocking method, must be called from another thread call_in_thread(do_long_job) reply_agree = message.create_reply() reply_agree.set_content('OK, I`ll do it, wait for me!') self.inform_behavior.send_agree(reply_agree)
def __init__(self, aid): super(AgentParticipant, self).__init__(aid) self.protocol = SubscribeParticipant(self) self.timed = Time(self, self.protocol.notify) self.behaviours.append(self.protocol) self.behaviours.append(self.timed) call_in_thread(my_time, 'a', 'b')
def handle_receive(self, *args): """Função automaticamente chamada quando mensagem do IED é recebida. Chama a função do agente de comunicação""" display_message(f'{self.id}@{self.ip}:{self.port}', f'Evento: {args}') # Assume que a presença de XCBR é para a abertura da chave if 'XCBR' in args and 'BRKF' not in args: self.breaker_position = 'open' call_in_thread(self.callback, self, *args)
def __init__(self, aid): super(AgentPublisher, self).__init__(aid) self.protocol = PublisherProtocol(self) self.timed = Time(self, self.protocol.notify) self.behaviours.append(self.protocol) self.behaviours.append(self.timed) call_in_thread(my_time, 'a', 'b')