Python zmq.SNDMORE Examples
The following are 30
code examples of zmq.SNDMORE().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
zmq
, or try the search function
.
Example #1
Source File: test_socket.py From pySINDy with MIT License | 6 votes |
def test_unicode_sockopts(self): """test setting/getting sockopts with unicode strings""" topic = "tést" if str is not unicode: topic = topic.decode('utf8') p,s = self.create_bound_pair(zmq.PUB, zmq.SUB) self.assertEqual(s.send_unicode, s.send_unicode) self.assertEqual(p.recv_unicode, p.recv_unicode) self.assertRaises(TypeError, s.setsockopt, zmq.SUBSCRIBE, topic) self.assertRaises(TypeError, s.setsockopt, zmq.IDENTITY, topic) s.setsockopt_unicode(zmq.IDENTITY, topic, 'utf16') self.assertRaises(TypeError, s.setsockopt, zmq.AFFINITY, topic) s.setsockopt_unicode(zmq.SUBSCRIBE, topic) self.assertRaises(TypeError, s.getsockopt_unicode, zmq.AFFINITY) self.assertRaisesErrno(zmq.EINVAL, s.getsockopt_unicode, zmq.SUBSCRIBE) identb = s.getsockopt(zmq.IDENTITY) identu = identb.decode('utf16') identu2 = s.getsockopt_unicode(zmq.IDENTITY, 'utf16') self.assertEqual(identu, identu2) time.sleep(0.1) # wait for connection/subscription p.send_unicode(topic,zmq.SNDMORE) p.send_unicode(topic*2, encoding='latin-1') self.assertEqual(topic, s.recv_unicode()) self.assertEqual(topic*2, s.recv_unicode(encoding='latin-1'))
Example #2
Source File: imagezmq.py From imagezmq with MIT License | 6 votes |
def send_array(self, A, msg='NoName', flags=0, copy=True, track=False): """Sends a numpy array with metadata and text message. Sends a numpy array with the metadata necessary for reconstructing the array (dtype,shape). Also sends a text msg, often the array or image name. Arguments: A: numpy array or OpenCV image. msg: (optional) array name, image name or text message. flags: (optional) zmq flags. copy: (optional) zmq copy flag. track: (optional) zmq track flag. """ md = dict( msg=msg, dtype=str(A.dtype), shape=A.shape, ) self.send_json(md, flags | zmq.SNDMORE) return self.send(A, flags, copy=copy, track=track)
Example #3
Source File: imagezmq.py From imagezmq with MIT License | 6 votes |
def send_jpg(self, msg='NoName', jpg_buffer=b'00', flags=0, copy=True, track=False): """Send a jpg buffer with a text message. Sends a jpg bytestring of an OpenCV image. Also sends text msg, often the image name. Arguments: msg: image name or text message. jpg_buffer: jpg buffer of compressed image to be sent. flags: (optional) zmq flags. copy: (optional) zmq copy flag. track: (optional) zmq track flag. """ md = dict(msg=msg, ) self.send_json(md, flags | zmq.SNDMORE) return self.send(jpg_buffer, flags, copy=copy, track=track)
Example #4
Source File: imagezmqtest.py From imagezmq with MIT License | 6 votes |
def send_array(self, A, msg='NoName', flags=0, copy=True, track=False): """Sends a numpy array with metadata and text message. Sends a numpy array with the metadata necessary for reconstructing the array (dtype,shape). Also sends a text msg, often the array or image name. Arguments: A: numpy array or OpenCV image. msg: (optional) array name, image name or text message. flags: (optional) zmq flags. copy: (optional) zmq copy flag. track: (optional) zmq track flag. """ md = dict( msg=msg, dtype=str(A.dtype), shape=A.shape, ) self.send_json(md, flags | zmq.SNDMORE) return self.send(A, flags, copy=copy, track=track)
Example #5
Source File: tx.py From pybtcfork with MIT License | 6 votes |
def fetch_tx(self, testnet=False): if self.prev_tx not in self.cache: socket = self.get_socket(testnet=testnet) nonce = int_to_little_endian(random.randint(0, 2**32), 4) msg = b'blockchain.fetch_transaction2' socket.send(msg, zmq.SNDMORE) socket.send(nonce, zmq.SNDMORE) socket.send(self.prev_tx[::-1]) response_msg = socket.recv() response_nonce = socket.recv() if response_msg != msg or response_nonce != nonce: raise RuntimeError('received wrong msg: {}'.format( response_msg.decode('ascii'))) response_tx = socket.recv() response_code = little_endian_to_int(response_tx[:4]) if response_code != 0: raise RuntimeError('got code from server: {}'.format(response_code)) stream = BytesIO(response_tx[4:]) self.cache[self.prev_tx] = Tx.parse(stream) return self.cache[self.prev_tx]
Example #6
Source File: imagezmqtest.py From imagezmq with MIT License | 6 votes |
def send_jpg(self, msg='NoName', jpg_buffer=b'00', flags=0, copy=True, track=False): """Send a jpg buffer with a text message. Sends a jpg bytestring of an OpenCV image. Also sends text msg, often the image name. Arguments: msg: image name or text message. jpg_buffer: jpg buffer of compressed image to be sent. flags: (optional) zmq flags. copy: (optional) zmq copy flag. track: (optional) zmq track flag. """ md = dict(msg=msg, ) self.send_json(md, flags | zmq.SNDMORE) return self.send(jpg_buffer, flags, copy=copy, track=track)
Example #7
Source File: __init__.py From embedding-as-service with MIT License | 6 votes |
def run(self): """ Main execution. Returns: """ # Socket to communicate with front facing server. socket = self.zmq_context.socket(zmq.DEALER) socket.connect('inproc://backend') while True: # First string recieved is socket ID of client client_id = socket.recv() request = socket.recv() # print('Worker ID - %s. Recieved computation request.' % (self.worker_id)) result = self.compute(request) # print('Worker ID - %s. Sending computed result back.' % (self.worker_id)) # For successful routing of result to correct client, the socket ID of client should be sent first. socket.send(client_id, zmq.SNDMORE) socket.send_string(result)
Example #8
Source File: test_socket.py From vnpy_crypto with MIT License | 6 votes |
def test_unicode_sockopts(self): """test setting/getting sockopts with unicode strings""" topic = "tést" if str is not unicode: topic = topic.decode('utf8') p,s = self.create_bound_pair(zmq.PUB, zmq.SUB) self.assertEqual(s.send_unicode, s.send_unicode) self.assertEqual(p.recv_unicode, p.recv_unicode) self.assertRaises(TypeError, s.setsockopt, zmq.SUBSCRIBE, topic) self.assertRaises(TypeError, s.setsockopt, zmq.IDENTITY, topic) s.setsockopt_unicode(zmq.IDENTITY, topic, 'utf16') self.assertRaises(TypeError, s.setsockopt, zmq.AFFINITY, topic) s.setsockopt_unicode(zmq.SUBSCRIBE, topic) self.assertRaises(TypeError, s.getsockopt_unicode, zmq.AFFINITY) self.assertRaisesErrno(zmq.EINVAL, s.getsockopt_unicode, zmq.SUBSCRIBE) identb = s.getsockopt(zmq.IDENTITY) identu = identb.decode('utf16') identu2 = s.getsockopt_unicode(zmq.IDENTITY, 'utf16') self.assertEqual(identu, identu2) time.sleep(0.1) # wait for connection/subscription p.send_unicode(topic,zmq.SNDMORE) p.send_unicode(topic*2, encoding='latin-1') self.assertEqual(topic, s.recv_unicode()) self.assertEqual(topic*2, s.recv_unicode(encoding='latin-1'))
Example #9
Source File: cig_olt_zmq.py From voltha with Apache License 2.0 | 5 votes |
def sync_send(self, data, flag=0): try: if flag == 1: self.socket.send(data, flags=zmq.SNDMORE) else: self.socket.send(data) if flag != 1: #try: thread.start_new_thread(self.sync_receive,()) #except: # log.exception(e.message) except Exception as e: log.exception(e.message)
Example #10
Source File: controller.py From platoon with MIT License | 5 votes |
def send_mb(self, arrays): """ Send a mini-batch over the socket. This function may block if arrays are being sent faster than the clients can handle. Parameters ---------- arrays : list of ndarrays List of :class:`numpy.ndarray` to send. All arrays should be contiguous for better performance. """ # The buffer protocol only works on contiguous arrays arrays = [numpy.ascontiguousarray(array) for array in arrays] headers = [numpy.lib.format.header_data_from_array_1_0(array) for array in arrays] self.asocket.send_json(headers, zmq.SNDMORE) for array in arrays[:-1]: self.asocket.send(array, zmq.SNDMORE) self.asocket.send(arrays[-1]) ################################################################################ # Helper functions # ################################################################################
Example #11
Source File: paramclient.py From hfsoftmax with MIT License | 5 votes |
def set_value_by_rows(self, mid, rows, data): msg = dict( op='set_value_by_rows', mid=mid, ) self._socket.send_json(msg, zmq.SNDMORE) self._send_array(rows, zmq.SNDMORE) self._send_array(data)
Example #12
Source File: paramclient.py From hfsoftmax with MIT License | 5 votes |
def get_value_by_rows(self, mid, rows): msg = dict(op='get_value_by_rows', mid=mid) self._socket.send_json(msg, zmq.SNDMORE) self._send_array(rows) # receive data meta = None while True: sockets = dict(self._poll.poll(1000)) if self._socket in sockets: msg = self._socket.recv() if not meta: meta = json.loads(msg) else: data = np.frombuffer(msg, dtype=meta['dtype']) return data.reshape(meta['shape'])
Example #13
Source File: paramclient.py From hfsoftmax with MIT License | 5 votes |
def set_matrix(self, mid, data, force=False): msg = dict(op='set_matrix', mid=mid, force=force) self._socket.send_json(msg, zmq.SNDMORE) self._send_array(data)
Example #14
Source File: paramclient.py From hfsoftmax with MIT License | 5 votes |
def _send_array(self, data, flags=0, copy=True, track=False): """send a numpy array with metadata""" md = dict( dtype=str(data.dtype), shape=data.shape, ) self._socket.send_json(md, flags | zmq.SNDMORE) return self._socket.send(data, flags, copy=copy, track=track)
Example #15
Source File: paramserver.py From hfsoftmax with MIT License | 5 votes |
def _send(self, ident, data): assert isinstance(data, np.ndarray) meta = {'dtype': str(data.dtype), 'shape': data.shape} self._socket.send(ident, zmq.SNDMORE) self._socket.send_json(meta) self._socket.send_multipart([ident, data])
Example #16
Source File: socket.py From Computable with MIT License | 5 votes |
def send_multipart(self, msg_parts, flags=0, copy=True, track=False): """send a sequence of buffers as a multipart message The zmq.SNDMORE flag is added to all msg parts before the last. Parameters ---------- msg_parts : iterable A sequence of objects to send as a multipart message. Each element can be any sendable object (Frame, bytes, buffer-providers) flags : int, optional SNDMORE is handled automatically for frames before the last. copy : bool, optional Should the frame(s) be sent in a copying or non-copying manner. track : bool, optional Should the frame(s) be tracked for notification that ZMQ has finished with it (ignored if copy=True). Returns ------- None : if copy or not track MessageTracker : if track and not copy a MessageTracker object, whose `pending` property will be True until the last send is completed. """ for msg in msg_parts[:-1]: self.send(msg, SNDMORE|flags, copy=copy, track=track) # Send the last part without the extra SNDMORE flag. return self.send(msg_parts[-1], flags, copy=copy, track=track)
Example #17
Source File: test_convert_ilsvrc2010.py From fuel with MIT License | 5 votes |
def test_train_set_producer(): tar_data, names, jpeg_names = create_fake_tar_of_tars(20150923, 5, min_num_images=45, max_num_images=55) all_jpegs = numpy.array(sum(jpeg_names, [])) numpy.random.RandomState(20150923).shuffle(all_jpegs) patched_files = all_jpegs[:10] patches_data = create_fake_patch_images(filenames=patched_files, num_train=10, num_valid=0, num_test=0) train_patches = extract_patch_images(io.BytesIO(patches_data), 'train') socket = MockSocket(zmq.PUSH) wnid_map = dict(zip((n.split('.')[0] for n in names), range(len(names)))) train_set_producer(socket, io.BytesIO(tar_data), io.BytesIO(patches_data), wnid_map) tar_data, names, jpeg_names = create_fake_tar_of_tars(20150923, 5, min_num_images=45, max_num_images=55) for tar_name in names: with tarfile.open(fileobj=io.BytesIO(tar_data)) as outer_tar: with tarfile.open(fileobj=outer_tar.extractfile(tar_name)) as tar: for record in tar: jpeg = record.name metadata_msg = socket.sent.popleft() assert metadata_msg['type'] == 'send_pyobj' assert metadata_msg['flags'] == zmq.SNDMORE key = tar_name.split('.')[0] assert metadata_msg['obj'] == (jpeg, wnid_map[key]) image_msg = socket.sent.popleft() assert image_msg['type'] == 'send' assert image_msg['flags'] == 0 if jpeg in train_patches: assert image_msg['data'] == train_patches[jpeg] else: image_data, _ = load_from_tar_or_patch(tar, jpeg, train_patches) assert image_msg['data'] == image_data
Example #18
Source File: ilsvrc2010.py From fuel with MIT License | 5 votes |
def other_set_producer(socket, which_set, image_archive, patch_archive, groundtruth): """Push image files read from the valid/test set TAR to a socket. Parameters ---------- socket : :class:`zmq.Socket` PUSH socket on which to send images. which_set : str Which set of images is being processed. One of 'train', 'valid', 'test'. Used for extracting the appropriate images from the patch archive. image_archive : str or file-like object The filename or file-handle for the TAR archive containing images. patch_archive : str or file-like object Filename or file handle for the TAR archive of patch images. groundtruth : iterable Iterable container containing scalar 0-based class index for each image, sorted by filename. """ patch_images = extract_patch_images(patch_archive, which_set) num_patched = 0 with tar_open(image_archive) as tar: filenames = sorted(info.name for info in tar if info.isfile()) images = (load_from_tar_or_patch(tar, filename, patch_images) for filename in filenames) pathless_filenames = (os.path.split(fn)[-1] for fn in filenames) image_iterator = equizip(images, pathless_filenames, groundtruth) for (image_data, patched), filename, class_index in image_iterator: if patched: num_patched += 1 socket.send_pyobj((filename, class_index), zmq.SNDMORE) socket.send(image_data, copy=False) if num_patched != len(patch_images): raise Exception
Example #19
Source File: ilsvrc2010.py From fuel with MIT License | 5 votes |
def image_consumer(socket, hdf5_file, num_expected, shuffle_seed=None, offset=0): """Fill an HDF5 file with incoming images from a socket. Parameters ---------- socket : :class:`zmq.Socket` PULL socket on which to receive images. hdf5_file : :class:`h5py.File` instance HDF5 file handle to which to write. Assumes `features`, `targets` and `filenames` already exist and have first dimension larger than `sum(images_per_class)`. num_expected : int The number of items we expect to be sent over the socket. shuffle_seed : int or sequence, optional Seed for a NumPy random number generator that permutes the images on disk. offset : int, optional The offset in the HDF5 datasets at which to start writing received examples. Defaults to 0. """ with progress_bar('images', maxval=num_expected) as pb: if shuffle_seed is None: index_gen = iter(xrange(num_expected)) else: rng = numpy.random.RandomState(shuffle_seed) index_gen = iter(rng.permutation(num_expected)) for i, num in enumerate(index_gen): image_filename, class_index = socket.recv_pyobj(zmq.SNDMORE) image_data = numpy.fromstring(socket.recv(), dtype='uint8') _write_to_hdf5(hdf5_file, num + offset, image_filename, image_data, class_index) pb.update(i + 1)
Example #20
Source File: ilsvrc2010.py From fuel with MIT License | 5 votes |
def train_set_producer(socket, train_archive, patch_archive, wnid_map): """Load/send images from the training set TAR file or patch images. Parameters ---------- socket : :class:`zmq.Socket` PUSH socket on which to send loaded images. train_archive : str or file-like object Filename or file handle for the TAR archive of training images. patch_archive : str or file-like object Filename or file handle for the TAR archive of patch images. wnid_map : dict A dictionary that maps WordNet IDs to 0-based class indices. Used to decode the filenames of the inner TAR files. """ patch_images = extract_patch_images(patch_archive, 'train') num_patched = 0 with tar_open(train_archive) as tar: for inner_tar_info in tar: with tar_open(tar.extractfile(inner_tar_info.name)) as inner: wnid = inner_tar_info.name.split('.')[0] class_index = wnid_map[wnid] filenames = sorted(info.name for info in inner if info.isfile()) images_gen = (load_from_tar_or_patch(inner, filename, patch_images) for filename in filenames) pathless_filenames = (os.path.split(fn)[-1] for fn in filenames) stream = equizip(pathless_filenames, images_gen) for image_fn, (image_data, patched) in stream: if patched: num_patched += 1 socket.send_pyobj((image_fn, class_index), zmq.SNDMORE) socket.send(image_data) if num_patched != len(patch_images): raise ValueError('not all patch images were used')
Example #21
Source File: server.py From fuel with MIT License | 5 votes |
def send_arrays(socket, arrays, stop=False): """Send NumPy arrays using the buffer interface and some metadata. Parameters ---------- socket : :class:`zmq.Socket` The socket to send data over. arrays : list A list of :class:`numpy.ndarray` to transfer. stop : bool, optional Instead of sending a series of NumPy arrays, send a JSON object with a single `stop` key. The :func:`recv_arrays` will raise ``StopIteration`` when it receives this. Notes ----- The protocol is very simple: A single JSON object describing the array format (using the same specification as ``.npy`` files) is sent first. Subsequently the arrays are sent as bytestreams (through NumPy's support of the buffering protocol). """ if arrays: # The buffer protocol only works on contiguous arrays arrays = [numpy.ascontiguousarray(array) for array in arrays] if stop: headers = {'stop': True} socket.send_json(headers) else: headers = [header_data_from_array_1_0(array) for array in arrays] socket.send_json(headers, zmq.SNDMORE) for array in arrays[:-1]: socket.send(array, zmq.SNDMORE) socket.send(arrays[-1])
Example #22
Source File: scheduler.py From Computable with MIT License | 5 votes |
def submit_task(self, job, indices=None): """Submit a task to any of a subset of our targets.""" if indices: loads = [self.loads[i] for i in indices] else: loads = self.loads idx = self.scheme(loads) if indices: idx = indices[idx] target = self.targets[idx] # print (target, map(str, msg[:3])) # send job to the engine self.engine_stream.send(target, flags=zmq.SNDMORE, copy=False) self.engine_stream.send_multipart(job.raw_msg, copy=False) # update load self.add_job(idx) self.pending[target][job.msg_id] = job # notify Hub content = dict(msg_id=job.msg_id, engine_id=target.decode('ascii')) self.session.send(self.mon_stream, 'task_destination', content=content, ident=[b'tracktask',self.ident]) #----------------------------------------------------------------------- # Result Handling #-----------------------------------------------------------------------
Example #23
Source File: utils.py From cryptotrader with MIT License | 5 votes |
def send_array(socket, A, flags=0, copy=False, track=False, block=True): """send a numpy array with metadata""" md = dict( dtype=str(A.dtype), shape=A.shape, ) if block: socket.send_json(md, flags | zmq.SNDMORE) return socket.send(A, flags, copy=copy, track=track) else: try: socket.send_json(md, flags | zmq.SNDMORE | zmq.NOBLOCK) return socket.send(A, flags| zmq.NOBLOCK, copy=copy, track=track) except zmq.Again: return False
Example #24
Source File: supvisorszmq.py From supvisors with Apache License 2.0 | 5 votes |
def send_address_status(self, status): """ This method sends a serialized form of the address status through the socket. """ self.logger.trace('send RemoteStatus {}'.format(status)) self.socket.send_string(EventHeaders.ADDRESS, zmq.SNDMORE) self.socket.send_json(status.serial())
Example #25
Source File: utils.py From robotics-rl-srl with MIT License | 5 votes |
def sendMatrix(socket, mat): """ Send a numpy mat with metadata over zmq :param socket: :param mat: (numpy matrix) """ metadata = dict( dtype=str(mat.dtype), shape=mat.shape, ) # SNDMORE flag specifies this is a multi-part message socket.send_json(metadata, flags=zmq.SNDMORE) return socket.send(mat, flags=0, copy=True, track=False)
Example #26
Source File: server.py From research with BSD 3-Clause "New" or "Revised" License | 5 votes |
def send_arrays(socket, arrays, stop=False): """Send NumPy arrays using the buffer interface and some metadata. Parameters ---------- socket : :class:`zmq.Socket` The socket to send data over. arrays : list A list of :class:`numpy.ndarray` to transfer. stop : bool, optional Instead of sending a series of NumPy arrays, send a JSON object with a single `stop` key. The :func:`recv_arrays` will raise ``StopIteration`` when it receives this. Notes ----- The protocol is very simple: A single JSON object describing the array format (using the same specification as ``.npy`` files) is sent first. Subsequently the arrays are sent as bytestreams (through NumPy's support of the buffering protocol). """ if arrays: # The buffer protocol only works on contiguous arrays arrays = [numpy.ascontiguousarray(array) for array in arrays] if stop: headers = {'stop': True} socket.send_json(headers) else: headers = [header_data_from_array_1_0(array) for array in arrays] socket.send_json(headers, zmq.SNDMORE) for array in arrays[:-1]: socket.send(array, zmq.SNDMORE) socket.send(arrays[-1])
Example #27
Source File: supvisorszmq.py From supvisors with Apache License 2.0 | 5 votes |
def send_supvisors_status(self, status): """ This method sends a serialized form of the supvisors status through the socket. """ self.logger.trace('send SupvisorsStatus {}'.format(status)) self.socket.send_string(EventHeaders.SUPVISORS, zmq.SNDMORE) self.socket.send_json(status.serial())
Example #28
Source File: cig_olt_zmq.py From voltha with Apache License 2.0 | 5 votes |
def omci_send(self, data, flag=0): try: if flag == 1: self.socket_tx.send(data, flags=zmq.SNDMORE) else: self.socket_tx.send(data) except Exception as e: log.exception(e.message)
Example #29
Source File: cig_olt_zmq.py From voltha with Apache License 2.0 | 5 votes |
def async_send(self, data, flag=0): try: if flag == 1: self.socket.send(data, flags=zmq.SNDMORE) else: self.socket.send(data) except Exception as e: log.exception(e.message)
Example #30
Source File: cig_olt_zmq.py From voltha with Apache License 2.0 | 5 votes |
def packet_send(self, data, flag=0): try: if flag == 1: self.socket_tx.send(data, flags=zmq.SNDMORE) else: self.socket_tx.send(data) except Exception as e: log.exception(e.message)