def run_plotting_server(address, port): """ Address and port to listen on. :param address: :param port: :return: """ # Get the first available socket starting from portand communicate it with the client who started this server sock, port = get_socket(address=address, port=port) write_port_to_file(port) max_number_clients = 100 max_plot_batch_size = 20000 sock.listen(max_number_clients) print(port) print("Plotting Server is listening") # We want to save and rescue the current plot in case the plotting server receives a signal.SIGINT (2) killer = GracefulKiller() # The plotting server receives input in a queue and returns the plot_ids as a way of communicating that it has rendered the plot main_input_queue = Queue.Queue() return_queue = Queue.Queue() # Start accepting clients' communication requests t0 = threading.Thread(target=handle_socket_accepts, args=(sock, main_input_queue, return_queue, max_number_clients)) t0.setDaemon(True) t0.start() # If killed, save the current figure atexit.register(save_current_figure) # Now, we can accept plots in the main thread! while True: if killer.kill_now: # The server has received a signal.SIGINT (2), so we stop receiving plots and terminate break # Retrieve data points that might have come in in the mean-time: client_messages = _queue_get_all_no_wait(main_input_queue, max_plot_batch_size) # client_messages is a list of ClientMessage objects if len(client_messages) > 0: return_values = [] with hold_dbplots(): for client_msg in client_messages: # For each ClientMessage object # Take apart the received message, plot, and return the plot_id to the client who sent it plot_message = pickle.loads( client_msg.dbplot_message ) # A DBPlotMessage object (see plotting_client.py) plot_message.dbplot_args['draw_now'] = False dbplot(**plot_message.dbplot_args) return_values.append( (client_msg.client_address, plot_message.plot_id)) for client, plot_id in return_values: return_queue.put([client, plot_id]) else: time.sleep(0.1)
def listen_on_port(port=7000): ''' Sets up a thread that listens on a port forwards communication to a queue. Both, Queue and the port it listens on are returned :return: queue, port ''' sock, port = get_socket("0.0.0.0", port=port) sock.listen(1) main_input_queue = queue.Queue() t = threading.Thread(target=handle_socket_accepts,args=(sock, main_input_queue, None,1)) t.setDaemon(True) t.start() return main_input_queue, port
def test_check_if_port_is_free(): for ip_address in ip_addresses: if ip_address not in get_local_ips(): with raises(AssertionError): check_if_port_is_free(ip_address, 80) else: sock, port = get_socket(ip_address, 7005) # port is now not free: with raises(SocketServer.socket.error): check_if_port_is_free(ip_address, port) sock.close() # now no error to be expected check_if_port_is_free(ip_address, port)
def listen_on_port(port=7000): ''' Sets up a thread that listens on a port forwards communication to a queue. Both, Queue and the port it listens on are returned :return: queue, port ''' sock, port = get_socket("0.0.0.0", port=port) sock.listen(1) main_input_queue = queue.Queue() t = threading.Thread(target=handle_socket_accepts, args=(sock, main_input_queue, None, 1)) t.setDaemon(True) t.start() return main_input_queue, port
def test_check_if_port_is_free(): for ip_address in ip_addresses: if ip_address not in get_local_ips(): with raises(AssertionError): check_if_port_is_free(ip_address,80) else: sock, port = get_socket(ip_address,7005) # port is now not free: with raises(SocketServer.socket.error): check_if_port_is_free(ip_address, port) sock.close() # now no error to be expected check_if_port_is_free(ip_address,port)
def run_plotting_server(address, port, client_address, client_port): """ Address and port to listen on. :param address: :param port: :return: """ # Get the first available socket starting from portand communicate it with the client who started this server sock, port = get_socket(address=address, port=port) write_port_to_file(port) max_number_clients = 100 max_plot_batch_size = 2000 sock.listen(max_number_clients) one_time_send_to(address=client_address,port=client_port,message=str(port)) # We want to save and rescue the current plot in case the plotting server receives a signal.SIGINT (2) killer = GracefulKiller() # The plotting server receives input in a queue and returns the plot_ids as a way of communicating that it has rendered the plot main_input_queue = Queue.Queue() return_queue = Queue.Queue() # Start accepting clients' communication requests t0 = threading.Thread(target=handle_socket_accepts,args=(sock, main_input_queue, return_queue, max_number_clients)) t0.setDaemon(True) t0.start() # Received exp_dir on first db_plot_message? exp_dir_received = False # Now, we can accept plots in the main thread! set_dbplot_figure_size(9,10) while True: if killer.kill_now: sock.close() # will cause handle_socket_accepts thread to terminate # The server has received a signal.SIGINT (2), so we stop receiving plots and terminate break # Retrieve data points that might have come in in the mean-time: client_messages = _queue_get_all_no_wait(main_input_queue, max_plot_batch_size) # client_messages is a list of ClientMessage objects if len(client_messages) > 0: return_values = [] with hold_dbplots(): for client_msg in client_messages: # For each ClientMessage object # Take apart the received message, plot, and return the plot_id to the client who sent it plot_message = pickle.loads(client_msg.dbplot_message) # A DBPlotMessage object (see plotting_client.py) plot_message.dbplot_args['draw_now'] = False if not exp_dir_received: if "exp_dir" == plot_message.dbplot_args["name"]: atexit.register(save_current_figure,(plot_message.dbplot_args["data"])) exp_dir_received = True if len(client_messages) == 1: continue else: continue axis = dbplot(**plot_message.dbplot_args) axis.ticklabel_format(style='sci', useOffset=False) return_values.append((client_msg.client_address, plot_message.plot_id)) if not exp_dir_received: atexit.register(save_current_figure) exp_dir_received = True plt.rcParams.update({'axes.titlesize': 'small', 'axes.labelsize': 'small'}) plt.subplots_adjust(hspace=0.4,wspace=0.6) for client, plot_id in return_values: return_queue.put([client,plot_id]) else: time.sleep(0.1)