engine.py
242 lines
| 9.8 KiB
| text/x-python
|
PythonLexer
|
r3539 | """A simple engine that talks to a controller over 0MQ. | ||
it handles registration, etc. and launches a kernel | ||||
|
r3644 | connected to the Controller's Schedulers. | ||
|
r4018 | |||
Authors: | ||||
* Min RK | ||||
|
r3539 | """ | ||
|
r3660 | #----------------------------------------------------------------------------- | ||
# Copyright (C) 2010-2011 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
|
r3552 | from __future__ import print_function | ||
|
r3631 | |||
|
r3539 | import sys | ||
import time | ||||
|
r4585 | from getpass import getpass | ||
|
r3539 | |||
import zmq | ||||
from zmq.eventloop import ioloop, zmqstream | ||||
|
r4585 | from IPython.external.ssh import tunnel | ||
|
r3603 | # internal | ||
|
r4585 | from IPython.utils.traitlets import ( | ||
|
r5344 | Instance, Dict, Integer, Type, CFloat, Unicode, CBytes, Bool | ||
|
r4585 | ) | ||
|
r6813 | from IPython.utils.py3compat import cast_bytes | ||
|
r3569 | |||
|
r3673 | from IPython.parallel.controller.heartmonitor import Heart | ||
from IPython.parallel.factory import RegistrationFactory | ||||
|
r6813 | from IPython.parallel.util import disambiguate_url | ||
|
r3673 | |||
|
r4006 | from IPython.zmq.session import Message | ||
|
r8079 | from IPython.zmq.ipkernel import Kernel, IPKernelApp | ||
|
r3539 | |||
|
r3604 | class EngineFactory(RegistrationFactory): | ||
|
r3539 | """IPython engine""" | ||
|
r4872 | |||
|
r3603 | # configurables: | ||
|
r3985 | out_stream_factory=Type('IPython.zmq.iostream.OutStream', config=True, | ||
help="""The OutStream for handling stdout/err. | ||||
Typically 'IPython.zmq.iostream.OutStream'""") | ||||
|
r4067 | display_hook_factory=Type('IPython.zmq.displayhook.ZMQDisplayHook', config=True, | ||
|
r3985 | help="""The class for handling displayhook. | ||
|
r4067 | Typically 'IPython.zmq.displayhook.ZMQDisplayHook'""") | ||
|
r3988 | location=Unicode(config=True, | ||
|
r3985 | help="""The location (an IP address) of the controller. This is | ||
used for disambiguating URLs, to determine whether | ||||
loopback should be used to connect or the public address.""") | ||||
timeout=CFloat(2,config=True, | ||||
help="""The time (in seconds) to wait for the Controller to respond | ||||
to registration requests before giving up.""") | ||||
|
r4585 | sshserver=Unicode(config=True, | ||
help="""The SSH server to use for tunneling connections to the Controller.""") | ||||
sshkey=Unicode(config=True, | ||||
|
r4589 | help="""The SSH private key file to use when tunneling connections to the Controller.""") | ||
|
r4585 | paramiko=Bool(sys.platform == 'win32', config=True, | ||
help="""Whether to use paramiko instead of openssh for tunnels.""") | ||||
|
r4872 | |||
|
r3604 | # not configurable: | ||
|
r3985 | user_ns=Dict() | ||
|
r5344 | id=Integer(allow_none=True) | ||
|
r3604 | registrar=Instance('zmq.eventloop.zmqstream.ZMQStream') | ||
kernel=Instance(Kernel) | ||||
|
r4872 | |||
|
r4160 | bident = CBytes() | ||
ident = Unicode() | ||||
def _ident_changed(self, name, old, new): | ||||
|
r6813 | self.bident = cast_bytes(new) | ||
|
r4585 | using_ssh=Bool(False) | ||
|
r4872 | |||
|
r3603 | def __init__(self, **kwargs): | ||
|
r3604 | super(EngineFactory, self).__init__(**kwargs) | ||
|
r3985 | self.ident = self.session.session | ||
|
r4872 | |||
|
r4585 | def init_connector(self): | ||
"""construct connection function, which handles tunnels.""" | ||||
self.using_ssh = bool(self.sshkey or self.sshserver) | ||||
|
r4872 | |||
|
r4585 | if self.sshkey and not self.sshserver: | ||
# We are using ssh directly to the controller, tunneling localhost to localhost | ||||
self.sshserver = self.url.split('://')[1].split(':')[0] | ||||
|
r4872 | |||
|
r4585 | if self.using_ssh: | ||
if tunnel.try_passwordless_ssh(self.sshserver, self.sshkey, self.paramiko): | ||||
password=False | ||||
else: | ||||
password = getpass("SSH Password for %s: "%self.sshserver) | ||||
else: | ||||
password = False | ||||
|
r4872 | |||
|
r4585 | def connect(s, url): | ||
url = disambiguate_url(url, self.location) | ||||
if self.using_ssh: | ||||
self.log.debug("Tunneling connection to %s via %s"%(url, self.sshserver)) | ||||
return tunnel.tunnel_connection(s, url, self.sshserver, | ||||
keyfile=self.sshkey, paramiko=self.paramiko, | ||||
password=password, | ||||
) | ||||
else: | ||||
return s.connect(url) | ||||
|
r4872 | |||
|
r4585 | def maybe_tunnel(url): | ||
"""like connect, but don't complete the connection (for use by heartbeat)""" | ||||
url = disambiguate_url(url, self.location) | ||||
if self.using_ssh: | ||||
self.log.debug("Tunneling connection to %s via %s"%(url, self.sshserver)) | ||||
url,tunnelobj = tunnel.open_tunnel(url, self.sshserver, | ||||
keyfile=self.sshkey, paramiko=self.paramiko, | ||||
password=password, | ||||
) | ||||
return url | ||||
return connect, maybe_tunnel | ||||
|
r4872 | |||
|
r3539 | def register(self): | ||
|
r3604 | """send the registration_request""" | ||
|
r4872 | |||
|
r4120 | self.log.info("Registering with controller at %s"%self.url) | ||
|
r4585 | ctx = self.context | ||
connect,maybe_tunnel = self.init_connector() | ||||
|
r4725 | reg = ctx.socket(zmq.DEALER) | ||
|
r4585 | reg.setsockopt(zmq.IDENTITY, self.bident) | ||
connect(reg, self.url) | ||||
self.registrar = zmqstream.ZMQStream(reg, self.loop) | ||||
|
r4872 | |||
|
r3540 | content = dict(queue=self.ident, heartbeat=self.ident, control=self.ident) | ||
|
r4585 | self.registrar.on_recv(lambda msg: self.complete_registration(msg, connect, maybe_tunnel)) | ||
|
r3575 | # print (self.session.key) | ||
|
r3539 | self.session.send(self.registrar, "registration_request",content=content) | ||
|
r4872 | |||
|
r4585 | def complete_registration(self, msg, connect, maybe_tunnel): | ||
|
r3539 | # print msg | ||
|
r3614 | self._abort_dc.stop() | ||
|
r3604 | ctx = self.context | ||
loop = self.loop | ||||
|
r4160 | identity = self.bident | ||
|
r3539 | idents,msg = self.session.feed_identities(msg) | ||
|
r4231 | msg = Message(self.session.unserialize(msg)) | ||
|
r4872 | |||
|
r3539 | if msg.content.status == 'ok': | ||
|
r3603 | self.id = int(msg.content.id) | ||
|
r4872 | |||
|
r4585 | # launch heartbeat | ||
hb_addrs = msg.content.heartbeat | ||||
|
r4872 | |||
|
r4585 | # possibly forward hb ports with tunnels | ||
hb_addrs = [ maybe_tunnel(addr) for addr in hb_addrs ] | ||||
heart = Heart(*map(str, hb_addrs), heart_id=identity) | ||||
heart.start() | ||||
|
r4872 | |||
|
r3604 | # create Shell Streams (MUX, Task, etc.): | ||
|
r3603 | queue_addr = msg.content.mux | ||
shell_addrs = [ str(queue_addr) ] | ||||
|
r3539 | task_addr = msg.content.task | ||
if task_addr: | ||||
|
r3569 | shell_addrs.append(str(task_addr)) | ||
|
r4872 | |||
|
r3657 | # Uncomment this to go back to two-socket model | ||
# shell_streams = [] | ||||
# for addr in shell_addrs: | ||||
|
r4725 | # stream = zmqstream.ZMQStream(ctx.socket(zmq.ROUTER), loop) | ||
|
r3657 | # stream.setsockopt(zmq.IDENTITY, identity) | ||
# stream.connect(disambiguate_url(addr, self.location)) | ||||
# shell_streams.append(stream) | ||||
|
r4872 | |||
|
r3657 | # Now use only one shell stream for mux and tasks | ||
|
r4725 | stream = zmqstream.ZMQStream(ctx.socket(zmq.ROUTER), loop) | ||
|
r3657 | stream.setsockopt(zmq.IDENTITY, identity) | ||
shell_streams = [stream] | ||||
|
r3604 | for addr in shell_addrs: | ||
|
r4585 | connect(stream, addr) | ||
|
r3657 | # end single stream-socket | ||
|
r4872 | |||
|
r3604 | # control stream: | ||
control_addr = str(msg.content.control) | ||||
|
r4725 | control_stream = zmqstream.ZMQStream(ctx.socket(zmq.ROUTER), loop) | ||
|
r3604 | control_stream.setsockopt(zmq.IDENTITY, identity) | ||
|
r4585 | connect(control_stream, control_addr) | ||
|
r4872 | |||
|
r3604 | # create iopub stream: | ||
iopub_addr = msg.content.iopub | ||||
|
r6804 | iopub_socket = ctx.socket(zmq.PUB) | ||
iopub_socket.setsockopt(zmq.IDENTITY, identity) | ||||
connect(iopub_socket, iopub_addr) | ||||
|
r4872 | |||
|
r6801 | # disable history: | ||
self.config.HistoryManager.hist_file = ':memory:' | ||||
# Redirect input streams and set a display hook. | ||||
|
r3605 | if self.out_stream_factory: | ||
|
r6804 | sys.stdout = self.out_stream_factory(self.session, iopub_socket, u'stdout') | ||
|
r6813 | sys.stdout.topic = cast_bytes('engine.%i.stdout' % self.id) | ||
|
r6804 | sys.stderr = self.out_stream_factory(self.session, iopub_socket, u'stderr') | ||
|
r6813 | sys.stderr.topic = cast_bytes('engine.%i.stderr' % self.id) | ||
|
r3605 | if self.display_hook_factory: | ||
|
r6804 | sys.displayhook = self.display_hook_factory(self.session, iopub_socket) | ||
|
r6813 | sys.displayhook.topic = cast_bytes('engine.%i.pyout' % self.id) | ||
|
r4160 | |||
|
r4872 | self.kernel = Kernel(config=self.config, int_id=self.id, ident=self.ident, session=self.session, | ||
|
r6804 | control_stream=control_stream, shell_streams=shell_streams, iopub_socket=iopub_socket, | ||
|
r6791 | loop=loop, user_ns=self.user_ns, log=self.log) | ||
|
r8079 | |||
|
r6834 | self.kernel.shell.display_pub.topic = cast_bytes('engine.%i.displaypub' % self.id) | ||
|
r4872 | |||
|
r8079 | # FIXME: This is a hack until IPKernelApp and IPEngineApp can be fully merged | ||
app = IPKernelApp(config=self.config, shell=self.kernel.shell, kernel=self.kernel, log=self.log) | ||||
app.init_profile_dir() | ||||
app.init_code() | ||||
|
r4872 | |||
|
r8079 | self.kernel.start() | ||
|
r3539 | else: | ||
|
r3614 | self.log.fatal("Registration Failed: %s"%msg) | ||
|
r3539 | raise Exception("Registration Failed: %s"%msg) | ||
|
r4872 | |||
|
r3610 | self.log.info("Completed registration with id %i"%self.id) | ||
|
r4872 | |||
|
r3614 | def abort(self): | ||
|
r3985 | self.log.fatal("Registration timed out after %.1f seconds"%self.timeout) | ||
|
r5218 | if self.url.startswith('127.'): | ||
|
r5205 | self.log.fatal(""" | ||
If the controller and engines are not on the same machine, | ||||
you will have to instruct the controller to listen on an external IP (in ipcontroller_config.py): | ||||
c.HubFactory.ip='*' # for all interfaces, internal and external | ||||
c.HubFactory.ip='192.168.1.101' # or any interface that the engines can see | ||||
or tunnel connections via ssh. | ||||
""") | ||||
|
r3604 | self.session.send(self.registrar, "unregistration_request", content=dict(id=self.id)) | ||
|
r3539 | time.sleep(1) | ||
|
r3614 | sys.exit(255) | ||
|
r4872 | |||
|
r3539 | def start(self): | ||
|
r3604 | dc = ioloop.DelayedCallback(self.register, 0, self.loop) | ||
dc.start() | ||||
|
r3614 | self._abort_dc = ioloop.DelayedCallback(self.abort, self.timeout*1000, self.loop) | ||
self._abort_dc.start() | ||||
|
r3604 | |||