def socket_server(port_or_path, single_socket_mode=False): instance_globals = { 'processes': {}, 'sockets': {'by_name': {}, 'by_path': {}}, 'values': {}, 'active_threads': [], 'inactive_threads': [], 'lock': Lock(), 'exit_if_idle': False, } def local_cleanup(): cleanup(instance_globals) atexit.register(local_cleanup) try: if isinstance(port_or_path, str): try: os.unlink(port_or_path) except OSError: if os.path.exists(port_or_path): raise s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: s.bind(port_or_path) except socket.error as e: raise Error('Bind failed: {0}'.format(e)) else: location = ('127.0.0.1', port_or_path) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: s.bind(location) except socket.error as e: raise Error('Bind failed or port {0}: {1}'.format(port_or_path, e)) except Exception as e: log.error(e) sys.exit(1) s.listen(5) log.info('Listening') while True: try: sock, addr = s.accept() log.info('Started client session with %s:%d', addr[0], addr[1]) container = [] instance_globals['exit_if_idle'] = False t = Thread(target=chat_with_a_client, args=(sock, addr, instance_globals, container)) container.append(t) instance_globals['active_threads'].append(t) t.daemon = True t.start() s.settimeout(.5) except socket.timeout: pass while instance_globals['inactive_threads']: addr, t = instance_globals['inactive_threads'].pop() t.join() log.info('Closed client session with %s:%d', addr[0], addr[1]) if not instance_globals['active_threads']: if single_socket_mode: break if instance_globals['exit_if_idle'] and is_idle(instance_globals): log.info('Exiting due to exit_if_idle request') break s.settimeout(None) s.close() papa_socket.cleanup(instance_globals) try: # noinspection PyUnresolvedReferences atexit.unregister(local_cleanup) except AttributeError: del instance_globals['lock']
def socket_server(port_or_path, single_socket_mode=False): instance_globals = { 'processes': {}, 'sockets': { 'by_name': {}, 'by_path': {} }, 'values': {}, 'active_threads': [], 'inactive_threads': [], 'lock': Lock(), 'exit_if_idle': False, } def local_cleanup(): cleanup(instance_globals) atexit.register(local_cleanup) try: if isinstance(port_or_path, str): try: os.unlink(port_or_path) except OSError: if os.path.exists(port_or_path): raise s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: s.bind(port_or_path) except socket.error as e: raise Error('Bind failed: {0}'.format(e)) else: location = ('127.0.0.1', port_or_path) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: s.bind(location) except socket.error as e: raise Error('Bind failed or port {0}: {1}'.format( port_or_path, e)) except Exception as e: log.exception(e) sys.exit(1) s.listen(5) log.info('Listening') while True: try: sock, addr = s.accept() log.info('Started client session with %s', addr) container = [] instance_globals['exit_if_idle'] = False t = Thread(target=chat_with_a_client, args=(sock, addr, instance_globals, container)) container.append(t) instance_globals['active_threads'].append(t) t.daemon = True t.start() s.settimeout(.5) except socket.timeout: pass while instance_globals['inactive_threads']: addr, t = instance_globals['inactive_threads'].pop() t.join() log.info('Closed client session with %s', addr) if not instance_globals['active_threads']: if single_socket_mode: break if instance_globals['exit_if_idle'] and is_idle(instance_globals): log.info('Exiting due to exit_if_idle request') break s.settimeout(None) s.close() papa_socket.cleanup(instance_globals) try: # noinspection PyUnresolvedReferences atexit.unregister(local_cleanup) except AttributeError: del instance_globals['lock']
def cleanup(instance_globals): if 'lock' in instance_globals: papa_socket.cleanup(instance_globals)