def test_connector_get_next_task_prefetch(): con = connector.Connector() con._base_request_uri = api_url con.prefetch = True assert con.get_next_task() == task1 assert con._tasks_in_progress == {task1['msg_id']: task1} assert con._tasks == [task2, task3] assert con.get_next_task() == task2 assert con._tasks_in_progress == { task1['msg_id']: task1, task2['msg_id']: task2 } assert con._tasks == [task3] assert con.get_next_task() == task3 assert con._tasks_in_progress == tasks_in_progress
def main(): """ Go into a while loop and wait for requests from Cluster. Returns: None """ while True: try: # Connect to the server faq = cluster.Connector() break except Exception: # Retry connecting until it succeeds pass # Go to an infinite loop of processing requests of the FAQ forum while True: try: # Get the request print("::: waiting for request") request = faq.get_next_task(timeout=None) print("::: received request") # Process the request ans = process(request) while True: try: # Answer to the request print("::: sending answer") print(request) print(ans) faq.reply(ans) print("::: answer sent") break except Exception: traceback.print_exc() print("::: could not send the answer") # Retry sending the reply until it succeeds pass except Exception: # Retry getting a request until it succeeds pass
def main(): con = connector.Connector("ws://localhost:39160/api/NLP/WS") while True: try: task = con.get_next_task() print(task) sent = False while not sent: print(con._tasks_in_progress) reply = generate_response(task) print("Response: " + str(reply)) try: if reply != "" and len(reply) > 0: con.reply(reply) sent = True except TypeError as e: print("Could not process input. TypeError: ") print(e) except json.JSONDecodeError as e: print("Could not process input. JSONDecodeError") print(e) except KeyboardInterrupt: con.close() break
def test_connector_reply(i): con = connector.Connector() con._base_request_uri = api_url con.get_next_task() assert con.reply(replies[i]) == expected_responses[i] assert replies[i]['msg_id'] not in con._tasks_in_progress.keys()