def send_to_worker(chunk_index, chunk1, chunk2=None): worker_index = queue.get() connection = connections[worker_index] connection.send(chunk_index) connection.send_bytes(chunk1) if chunk2 is not None: connection.send_bytes(chunk2)
def run(self): while True: connection = self._listener.accept() class Closer: def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): connection.close() with Closer(): while True: try: message = connection.recv() except EOFError: break try: result = self._handle_message(message) except self._AppException: result = { 'status': 'failure', 'traceback': traceback.format_exception(*sys.exc_info()), } print(result) connection.send(result)
def run(self): if self.stdin_fd != -1: sys.stdin.close() sys.stdin = os.fdopen(self.stdin_fd) try: with xopen(self.file, 'rb') as f: if self.file2: with xopen(self.file2, 'rb') as f2: for chunk_index, (chunk1, chunk2) in enumerate( dnaio.read_paired_chunks( f, f2, self.buffer_size)): self.send_to_worker(chunk_index, chunk1, chunk2) else: for chunk_index, chunk in enumerate( dnaio.read_chunks(f, self.buffer_size)): self.send_to_worker(chunk_index, chunk) # Send poison pills to all workers for _ in range(len(self.connections)): worker_index = self.queue.get() self.connections[worker_index].send(-1) except Exception as e: # TODO better send this to a common "something went wrong" Queue for connection in self.connections: connection.send(-2) connection.send((e, traceback.format_exc()))
def handle_connection(self, connection): try: while True: func_name, args, kwargs = pickle.loads(connection.recv().encode()) try: result = self._functions[func_name](*args, **kwargs) connection.send(pickle.dumps(result)) except Exception as e: connection.send(pickle.dumps(e)) except EOFError: pass
def _export(source, schema, out_prefix, part=None, connection=None): df = Exporter.set_date_columns_on_dataframe(source, schema) outfile = "{}.{}parquet.gz".format(out_prefix, f"part.{part}." if part else "") df.to_parquet( outfile, engine="fastparquet", compression="gzip", times="int96", ) if connection: connection.send(outfile) return outfile
def sendmessage(connection, msg, t_step, stop_event): stime = time.time() notes = msg[0] for inote in notes: #outport.send(mido.Message('note_on', note=inote, velocity=msg[2], # channel=msg[3])) connection.send(('note_on', msg[3], inote, msg[2])) while time.time() - stime < msg[1] - 8 * t_step and not stop_event.is_set( ): time.sleep(config.SLEEPTIME) for inote in notes: #outport.send(mido.Message('note_off', note=inote, channel=msg[3])) connection.send(('note_off', msg[3], inote, msg[2]))
def send(self, obj): for connection in self.send_connections.values(): connection.send(obj)
import multiprocessing import multiprocessing.connection rx = multiprocessing.connection.Client(('localhost', 12345)).recv() # ruleid: multiprocessing-recv connection = multiprocessing.connection.Client( ('localhost', 12345), ) output = {} connection.send(output) # toodoruleid:multiprocessing.recv rx = connection.recv()
def run(self): """ Run the thread, accepting connections and then listening on them until they are closed. Each message is a call into the function table. """ logger.debug("server listening on '%s'", self.pipe) while self._is_closed is False: logger.debug("server thread is about to create a server") ready = False # test to see if there is a connection waiting on the pipe if sys.platform == "win32": try: mpc_win32.WaitNamedPipe(self.server.address, self.LISTEN_TIMEOUT * 1000) ready = True except WindowsError as e: logger.debug("Error during WaitNamedPipe:", exc_info=True) if e.args[0] not in ( mpc_win32.ERROR_SEM_TIMEOUT, mpc_win32.ERROR_PIPE_BUSY, ): raise ready = False else: # can use select on osx and linux (rd, _, _) = select.select([self.server._listener._socket], [], [], self.LISTEN_TIMEOUT) ready = len(rd) > 0 if not ready: logger.debug("server thread could not create server") continue connection = None try: # connection waiting to be read, accept it logger.debug("server about to accept connection") connection = SafePickleConnection(self.server.accept()) logger.debug("server accepted connection") while self._is_closed is False: # test to see if there is data waiting on the connection has_data = connection.poll(self.LISTEN_TIMEOUT) # see if we need to stop the server if self._is_closed: return # If we timed out waiting for data, go back to sleep. if not has_data: continue # data coming over the connection is a tuple of (name, args, kwargs) (respond, func_name, args, kwargs) = pickle.loads(connection.recv()) logger.debug("server calling '%s(%s, %s)'" % (func_name, args, kwargs)) try: if func_name not in self._functions: logger.error( "unknown function call: '%s', expecting one of '%s'" % (func_name, self.list_functions())) raise ValueError("unknown function call: '%s'" % func_name) # grab the function from the function table func = self._functions[func_name] # execute the function on the main thread. It may do GUI work. result = self.engine.execute_in_main_thread( func, *args, **kwargs) # If the RPC server was stopped, don't bother trying to reply, the connection # will have been broken on the client side and this will avoid an error # on the server side when calling send. if self._SERVER_WAS_STOPPED != result: # if the client expects the results, send them along logger.debug("server got result '%s'" % result) if respond: connection.send(pickle.dumps(result)) except Exception as e: # if any of the above fails send the exception back to the client logger.error("got exception '%s'" % e) logger.debug(" traceback:\n%s" % traceback.format_exc()) if respond: connection.send(pickle.dumps(e)) except (EOFError, IOError): # let these errors go # just keep serving new connections pass finally: # It's possible we failed accepting, so the variable may not be defined. if connection is not None: logger.debug("server closing") connection.close() logger.debug("server closed") logger.debug("server thread shutting down")
print("You are now connected please wait...") address = ('127.0.0.1', 0x1234) connection = multiprocessing.connection.Client(address, authkey=b'secret') sym = connection.recv() valid = 1 while True: while valid != 0: newboard = connection.recv() if newboard == "P1 Wins!" or newboard == "P2 Wins!" or newboard == 9: break servermess = connection.recv() print(newboard) print(servermess) mymove = int(input("Enter number to place move: ")) playerData = {'move': mymove, 'sym': sym} connection.send(playerData) waitmess = connection.recv() updateboard = connection.recv() winstatus = connection.recv() if waitmess == "Invalid Entry, try again": print(updateboard) print(waitmess) valid = 1 else: print(updateboard) print(waitmess) break if newboard == "P1 Wins!" or newboard == "P2 Wins!" or newboard == 9: break if winstatus == "P1 Wins!" or winstatus == "P2 Wins!" or winstatus == 9:
def run(self): """ Run the thread, accepting connections and then listening on them until they are closed. Each message is a call into the function table. """ self._logger.debug("server listening on '%s'", self.pipe) while True: # test to see if there is a connection waiting on the pipe if sys.platform == "win32": # need to use win32 api for windows mpc_win32 = multiprocessing.connection.win32 try: mpc_win32.WaitNamedPipe(self.server.address, self.LISTEN_TIMEOUT*1000) ready = True except WindowsError, e: if e.args[0] not in (mpc_win32.ERROR_SEM_TIMEOUT, mpc_win32.ERROR_PIPE_BUSY): raise ready = False else: # can use select on osx and linux (rd, _, _) = select.select([self.server._listener._socket], [], [], self.LISTEN_TIMEOUT) ready = (len(rd) > 0) if not ready: # nothing ready, see if we need to stop the server, if not keep waiting if self._stop: break continue # connection waiting to be read, accept it connection = self.server.accept() self._logger.debug("server accepted connection") try: while True: # test to see if there is data waiting on the connection if not connection.poll(self.LISTEN_TIMEOUT): # no data waiting, see if we need to stop the server, if not keep waiting if self._stop: break continue # data coming over the connection is a tuple of (name, args, kwargs) (respond, func_name, args, kwargs) = pickle.loads(connection.recv()) self._logger.debug("server calling '%s(%s, %s)'" % (func_name, args, kwargs)) try: if func_name not in self._functions: self._logger.error("unknown function call: '%s'" % func_name) raise ValueError("unknown function call: '%s'" % func_name) # grab the function from the function table func = self._functions[func_name] # execute the function on the main thread. It may do GUI work. result = self.engine.execute_in_main_thread(func, *args, **kwargs) # if the client expects the results, send them along self._logger.debug("server got result '%s'" % result) if respond: connection.send(pickle.dumps(result)) except Exception as e: # if any of the above fails send the exception back to the client self._logger.error("got exception '%s'" % e) self._logger.debug(" traceback:\n%s" % traceback.format_exc()) if respond: connection.send(pickle.dumps(e)) except (EOFError, IOError): # let these errors go # just keep serving new connections pass finally: self._logger.debug("server closing") connection.close()
import multiprocessing.connection import pickle from django_load_balancer.load_balancer_factory import LoadBalancerFactory from django_load_balancer.local_settings import LOAD_BALANCER if __name__ == "__main__": load_balancer = LoadBalancerFactory.create_load_balancer() address = (LOAD_BALANCER['ADDRESS']['HOST'], LOAD_BALANCER['ADDRESS']['PORT']) listener = multiprocessing.connection.Listener( address, authkey=bytes(LOAD_BALANCER['ADDRESS']['AUTHKEY'], 'utf-8')) while True: connection = listener.accept() while True: payload = connection.recv() if payload == 'close': connection.close() break else: query = pickle.loads(payload) result = load_balancer.run_query(query) connection.send(result)