def kill(self): """Forcibly terminate the kernel. This method may be used to dispose of a kernel that won't shut down. Working kernels should usually be shut down by sending shutdown_request from a client and giving it some time to clean up. """ msg = Message.from_type('kill_request', {}) self.messaging.send('nanny_control', msg)
def interrupt(self): """Interrupt the kernel by sending it a signal or similar event Kernels can request to get interrupts as messages rather than signals. The manager is *not* expected to handle this. :meth:`.KernelClient.interrupt` should send an interrupt_request or call this method as appropriate. """ msg = Message.from_type('interrupt_request', {}) self.messaging.send('nanny_control', msg)
def handle_request(self, raw_msg): msg = self.messaging.session.deserialize(raw_msg) msg_type = msg.header['msg_type'] if msg_type not in self.allowed_requests: raise ValueError("Unknown request type: {!r}".format(msg_type)) reply_type = self.allowed_requests[msg_type] reply_content = getattr(self, msg_type)(msg) reply = Message.from_type(reply_type, reply_content, parent_msg=msg) self.messaging.send('nanny_control', reply)
def is_alive(self): """Check whether the kernel is currently alive (e.g. the process exists) """ msg = Message.from_type('is_alive_request', {}) self.messaging.send('nanny_control', msg) while True: self.messaging.nanny_control_socket.poll() reply = self.messaging.recv('nanny_control') if reply.parent_header['msg_id'] == msg.header['msg_id']: return reply.content['alive']
def test_datetimes(self): content = dict(t=utcnow()) metadata = dict(t=utcnow()) p = Message.from_type('msg', {}) msg = Message.from_type('msg', content, metadata=metadata, parent_msg=p) smsg = self.session.serialize(msg) msg2 = self.session.deserialize(smsg) assert isinstance(msg2.header['date'], datetime) assert msg.header == msg2.header assert msg.parent_header == msg2.parent_header assert msg.parent_header == msg2.parent_header assert isinstance(msg.content['t'], datetime) assert isinstance(msg.metadata['t'], datetime) assert isinstance(msg2.content['t'], string_types) assert isinstance(msg2.metadata['t'], string_types) assert msg.content == jsonutil.extract_dates(msg2.content) assert msg.metadata == jsonutil.extract_dates(msg2.metadata)
def test_serialize(self): msg = Message.from_type('execute', content=dict(a=10, b=1.1)) msg.idents = [b'foo'] msg_list = self.session.serialize(msg) new_msg = self.session.deserialize(msg_list) assert new_msg.idents == [b'foo'] assert new_msg.header == msg.header assert new_msg.content == msg.content assert new_msg.parent_header == msg.parent_header assert new_msg.metadata == msg.metadata # ensure floats don't come out as Decimal: assert type(new_msg.content['b']) == type(new_msg.content['b'])
def test_tracking(self): """test tracking messages""" a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR) messaging = _messaging_w_socket_pair(a, b) messaging.copy_threshold = 1 stream = ZMQStream(a) msg = Message.from_type('test_xmpl', {}) messaging.send('A', msg, track=False) assert msg.tracker is DONE messaging.send('A', msg, track=True) assert isinstance(msg.tracker, zmq.MessageTracker) msg2 = Message.from_type('test_xmpl', {}) msg2.buffers = [zmq.Frame(b'hi there', track=True, copy=False)] messaging.send('A', msg2, track=True) t = msg2.tracker assert isinstance(t, zmq.MessageTracker) assert t is not DONE with pytest.raises(zmq.NotDone): t.wait(.1) del msg2 t.wait(1)
def test_send(): ctx = zmq.Context.instance() A = ctx.socket(zmq.PAIR) B = ctx.socket(zmq.PAIR) A.bind("inproc://test") B.connect("inproc://test") messaging = _messaging_w_socket_pair(A, B) msg = Message.from_type('test_xmpl', dict(a=10)) msg.idents = [b'foo'] msg.buffers = [b'bar'] messaging.send('A', msg) new_msg = messaging.recv('B') assert new_msg.idents[0] == b'foo' assert new_msg.header == msg.header assert new_msg.content == msg.content assert new_msg.parent_header == msg.parent_header assert new_msg.metadata == msg.metadata assert new_msg.buffers == msg.buffers # buffers must support the buffer protocol msg.buffers = [1] with pytest.raises(TypeError): messaging.send('A', msg) # buffers must be contiguous buf = memoryview(os.urandom(16)) msg.buffers = [buf[::2]] with pytest.raises(ValueError): messaging.send('A', msg) A.close() B.close() ctx.term()
def msg(self, msg_type, content): """Create a v4 msg (same as v5, minus version header)""" msg = Message.from_type(msg_type, content).make_dict() msg['header'].pop('version') return msg
def msg(self, msg_type, content): return Message.from_type(msg_type, content).make_dict()
def on_kernel_dead(self): msg = Message.from_type('kernel_died', {}) self.messaging.send('nanny_events', msg) IOLoop.current().stop()