##// END OF EJS Templates
update edit page blocks after layout changes
update edit page blocks after layout changes

File last commit:

r17095:809e8f1e
r19318:3f7fd80e
Show More
ipengineapp.py
397 lines | 13.2 KiB | text/x-python | PythonLexer
MinRK
Refactor newparallel to use Config system...
r3604 #!/usr/bin/env python
# encoding: utf-8
"""
The IPython engine application
MinRK
update recently changed modules with Authors in docstring
r4018
Authors:
* Brian Granger
* MinRK
MinRK
Refactor newparallel to use Config system...
r3604 """
#-----------------------------------------------------------------------------
MinRK
update recently changed modules with Authors in docstring
r4018 # Copyright (C) 2008-2011 The IPython Development Team
MinRK
Refactor newparallel to use Config system...
r3604 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
MinRK
resort imports in a cleaner order
r3631 import json
MinRK
Refactor newparallel to use Config system...
r3604 import os
import sys
MinRK
allow engines to wait for url_files to arrive...
r4120 import time
MinRK
Refactor newparallel to use Config system...
r3604
import zmq
from zmq.eventloop import ioloop
MinRK
move ipcluster create|list to `ipython profile create|list`...
r4024 from IPython.core.profiledir import ProfileDir
MinRK
cleanup aliases in parallel apps...
r4115 from IPython.parallel.apps.baseapp import (
BaseParallelApplication,
base_aliases,
base_flags,
MinRK
catch_config -> catch_config_error
r5214 catch_config_error,
MinRK
cleanup aliases in parallel apps...
r4115 )
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 from IPython.kernel.zmq.log import EnginePUBHandler
Thomas Kluyver
Rename KernelBase & Kernel to Kernel & IPythonKernel
r17095 from IPython.kernel.zmq.ipkernel import IPythonKernel as Kernel
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 from IPython.kernel.zmq.kernelapp import IPKernelApp
from IPython.kernel.zmq.session import (
MinRK
enable HMAC message signing by default in kernels...
r4962 Session, session_aliases, session_flags
)
MinRK
mv IPython.zmq to IPython.kernel.zmq
r9372 from IPython.kernel.zmq.zmqshell import ZMQInteractiveShell
MinRK
Refactor newparallel to use Config system...
r3604
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 from IPython.config.configurable import Configurable
MinRK
enable HMAC message signing by default in kernels...
r4962
MinRK
organize IPython.parallel into subpackages
r3673 from IPython.parallel.engine.engine import EngineFactory
MinRK
use individual ports, rather than full urls in connection files
r7890 from IPython.parallel.util import disambiguate_ip_address
MinRK
organize IPython.parallel into subpackages
r3673
MinRK
Refactor newparallel to use Config system...
r3604 from IPython.utils.importstring import import_item
MinRK
discard parallel.util.asbytes in favor of py3compat.cast_bytes
r6813 from IPython.utils.py3compat import cast_bytes
MinRK
add listen_kernel method to IPEngineApp
r6887 from IPython.utils.traitlets import Bool, Unicode, Dict, List, Float, Instance
MinRK
Refactor newparallel to use Config system...
r3604
MinRK
persist connection data to disk as json
r3614
MinRK
Refactor newparallel to use Config system...
r3604 #-----------------------------------------------------------------------------
# Module level variables
#-----------------------------------------------------------------------------
MinRK
parallel docs, tests, default config updated to newconfig
r3990 _description = """Start an IPython engine for parallel computing.
MinRK
all ipcluster scripts in some degree of working order with new config
r3985
IPython engines run in parallel and perform computations on behalf of a client
and controller. A controller needs to be started before the engines. The
engine can be configured using command line options or using a cluster
directory. Cluster directories contain config, log and security files and are
MinRK
move ipcluster create|list to `ipython profile create|list`...
r4024 usually located in your ipython directory and named as "profile_name".
Brian E. Granger
Finishing up help string work.
r4218 See the `profile` and `profile-dir` options for details.
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 """
Brian Granger
More work on adding examples to help strings.
r4216 _examples = """
ipengine --ip=192.168.0.1 --port=1000 # connect to hub at ip and port
Brian E. Granger
Finishing up help string work.
r4218 ipengine --log-to-file --log-level=DEBUG # log to a file with DEBUG verbosity
Brian Granger
More work on adding examples to help strings.
r4216 """
MinRK
all ipcluster scripts in some degree of working order with new config
r3985
#-----------------------------------------------------------------------------
# MPI configuration
#-----------------------------------------------------------------------------
MinRK
Refactor newparallel to use Config system...
r3604
mpi4py_init = """from mpi4py import MPI as mpi
mpi.size = mpi.COMM_WORLD.Get_size()
mpi.rank = mpi.COMM_WORLD.Get_rank()
"""
pytrilinos_init = """from PyTrilinos import Epetra
class SimpleStruct:
pass
mpi = SimpleStruct()
mpi.rank = 0
mpi.size = 0
"""
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 class MPI(Configurable):
"""Configurable for MPI initialization"""
MinRK
cleanup parallel traits...
r3988 use = Unicode('', config=True,
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 help='How to enable MPI (mpi4py, pytrilinos, or empty string to disable).'
)
MinRK
Refactor newparallel to use Config system...
r3604
MinRK
parallel.apps cleanup per review...
r4850 def _use_changed(self, name, old, new):
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 # load default init script if it's not set
if not self.init_script:
self.init_script = self.default_inits.get(new, '')
MinRK
cleanup parallel traits...
r3988 init_script = Unicode('', config=True,
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 help="Initialization code for MPI")
default_inits = Dict({'mpi4py' : mpi4py_init, 'pytrilinos':pytrilinos_init},
config=True)
MinRK
Refactor newparallel to use Config system...
r3604
#-----------------------------------------------------------------------------
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 # Main application
MinRK
Refactor newparallel to use Config system...
r3604 #-----------------------------------------------------------------------------
MinRK
cleanup aliases in parallel apps...
r4115 aliases = dict(
file = 'IPEngineApp.url_file',
c = 'IPEngineApp.startup_command',
s = 'IPEngineApp.startup_script',
MinRK
Refactor newparallel to use Config system...
r3604
MinRK
cleanup aliases in parallel apps...
r4115 url = 'EngineFactory.url',
MinRK
add ssh tunneling to Engine...
r4585 ssh = 'EngineFactory.sshserver',
sshkey = 'EngineFactory.sshkey',
MinRK
cleanup aliases in parallel apps...
r4115 ip = 'EngineFactory.ip',
transport = 'EngineFactory.transport',
port = 'EngineFactory.regport',
location = 'EngineFactory.location',
timeout = 'EngineFactory.timeout',
mpi = 'MPI.use',
)
aliases.update(base_aliases)
MinRK
enable HMAC message signing by default in kernels...
r4962 aliases.update(session_aliases)
flags = {}
flags.update(base_flags)
flags.update(session_flags)
Brian Granger
More work on adding examples to help strings.
r4216
MinRK
update parallel apps to use ProfileDir
r3992 class IPEngineApp(BaseParallelApplication):
MinRK
all ipcluster scripts in some degree of working order with new config
r3985
MinRK
add cluster_id to parallel apps...
r4847 name = 'ipengine'
description = _description
Brian Granger
More work on adding examples to help strings.
r4216 examples = _examples
MinRK
add ZMQInteractiveShell to IPEngineApp class list...
r8571 classes = List([ZMQInteractiveShell, ProfileDir, Session, EngineFactory, Kernel, MPI])
MinRK
all ipcluster scripts in some degree of working order with new config
r3985
startup_script = Unicode(u'', config=True,
help='specify a script to be run at startup')
MinRK
cleanup parallel traits...
r3988 startup_command = Unicode('', config=True,
MinRK
Refactor newparallel to use Config system...
r3604 help='specify a command to be run at startup')
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 url_file = Unicode(u'', config=True,
help="""The full location of the file containing the connection information for
the controller. If this is not given, the file must be in the
security directory of the cluster directory. This location is
MinRK
update parallel apps to use ProfileDir
r3992 resolved using the `profile` or `profile_dir` options.""",
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 )
MinRK
allow engines to wait for url_files to arrive...
r4120 wait_for_url_file = Float(5, config=True,
help="""The maximum number of seconds to wait for url_file to exist.
This is useful for batch-systems and shared-filesystems where the
controller and engine are started at the same time and it
may take a moment for the controller to write the connector files.""")
MinRK
Refactor newparallel to use Config system...
r3604
MinRK
add cluster_id to parallel apps...
r4847 url_file_name = Unicode(u'ipcontroller-engine.json', config=True)
def _cluster_id_changed(self, name, old, new):
if new:
MinRK
parallel.apps cleanup per review...
r4850 base = 'ipcontroller-%s' % new
MinRK
add cluster_id to parallel apps...
r4847 else:
base = 'ipcontroller'
MinRK
parallel.apps cleanup per review...
r4850 self.url_file_name = "%s-engine.json" % base
MinRK
add cluster_id to parallel apps...
r4847
MinRK
re-enable log forwarding and iplogger
r3989 log_url = Unicode('', config=True,
help="""The URL for the iploggerapp instance, for forwarding
logging to a central location.""")
MinRK
add listen_kernel method to IPEngineApp
r6887
# an IPKernelApp instance, used to setup listening for shell frontends
kernel_app = Instance(IPKernelApp)
MinRK
Refactor newparallel to use Config system...
r3604
MinRK
cleanup aliases in parallel apps...
r4115 aliases = Dict(aliases)
MinRK
enable HMAC message signing by default in kernels...
r4962 flags = Dict(flags)
MinRK
expose kernel at app-level, so EngineApp looks like KernelApp
r6879
@property
def kernel(self):
"""allow access to the Kernel object, so I look like IPKernelApp"""
return self.engine.kernel
MinRK
Refactor newparallel to use Config system...
r3604
MinRK
persist connection data to disk as json
r3614 def find_url_file(self):
MinRK
allow engines to wait for url_files to arrive...
r4120 """Set the url file.
MinRK
Refactor newparallel to use Config system...
r3604
Here we don't try to actually see if it exists for is valid as that
is hadled by the connection logic.
"""
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 config = self.config
MinRK
Refactor newparallel to use Config system...
r3604 # Find the actual controller key file
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 if not self.url_file:
self.url_file = os.path.join(
MinRK
update parallel apps to use ProfileDir
r3992 self.profile_dir.security_dir,
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 self.url_file_name
MinRK
Refactor newparallel to use Config system...
r3604 )
MinRK
add ssh tunneling to Engine...
r4585
def load_connector_file(self):
"""load config from a JSON connector file,
at a *lower* priority than command-line/config files.
"""
MinRK
minor cleanup in ipcontroller/ipengine...
r5483 self.log.info("Loading url_file %r", self.url_file)
MinRK
add ssh tunneling to Engine...
r4585 config = self.config
with open(self.url_file) as f:
James Porter
Fix for race condition in url file decoding....
r13543 num_tries = 0
max_tries = 5
d = ""
while not d:
try:
d = json.loads(f.read())
except ValueError:
if num_tries > max_tries:
raise
num_tries += 1
time.sleep(0.5)
MinRK
add ssh tunneling to Engine...
r4585
MinRK
simplify IPython.parallel connections...
r7889 # allow hand-override of location for disambiguation
# and ssh-server
MinRK
AttributeError check on config no longer works...
r12796 if 'EngineFactory.location' not in config:
MinRK
add ssh tunneling to Engine...
r4585 config.EngineFactory.location = d['location']
MinRK
AttributeError check on config no longer works...
r12796 if 'EngineFactory.sshserver' not in config:
MinRK
simplify IPython.parallel connections...
r7889 config.EngineFactory.sshserver = d.get('ssh')
location = config.EngineFactory.location
MinRK
use individual ports, rather than full urls in connection files
r7890 proto, ip = d['interface'].split('://')
MinRK
add missing location when disambiguating controller IP...
r8156 ip = disambiguate_ip_address(ip, location)
MinRK
use individual ports, rather than full urls in connection files
r7890 d['interface'] = '%s://%s' % (proto, ip)
MinRK
simplify IPython.parallel connections...
r7889
MinRK
propagate signature_scheme config to parallel connection files
r11657 # DO NOT allow override of basic URLs, serialization, or key
MinRK
simplify IPython.parallel connections...
r7889 # JSON file takes top priority there
MinRK
propagate signature_scheme config to parallel connection files
r11657 config.Session.key = cast_bytes(d['key'])
config.Session.signature_scheme = d['signature_scheme']
MinRK
simplify IPython.parallel connections...
r7889
MinRK
use individual ports, rather than full urls in connection files
r7890 config.EngineFactory.url = d['interface'] + ':%i' % d['registration']
MinRK
simplify IPython.parallel connections...
r7889
config.Session.packer = d['pack']
config.Session.unpacker = d['unpack']
self.log.debug("Config changed:")
self.log.debug("%r", config)
self.connection_info = d
MinRK
add listen_kernel method to IPEngineApp
r6887
MinRK
add IPython.parallel.bind_kernel...
r6893 def bind_kernel(self, **kwargs):
"""Promote engine to listening kernel, accessible to frontends."""
MinRK
add listen_kernel method to IPEngineApp
r6887 if self.kernel_app is not None:
return
MinRK
add IPython.parallel.bind_kernel...
r6893 self.log.info("Opening ports for direct connections as an IPython kernel")
MinRK
add listen_kernel method to IPEngineApp
r6887
kernel = self.kernel
MinRK
add IPython.parallel.bind_kernel...
r6893 kwargs.setdefault('config', self.config)
kwargs.setdefault('log', self.log)
kwargs.setdefault('profile_dir', self.profile_dir)
kwargs.setdefault('session', self.engine.session)
app = self.kernel_app = IPKernelApp(**kwargs)
# allow IPKernelApp.instance():
IPKernelApp._instance = app
MinRK
add listen_kernel method to IPEngineApp
r6887 app.init_connection_file()
# relevant contents of init_sockets:
app.shell_port = app._bind_socket(kernel.shell_streams[0], app.shell_port)
app.log.debug("shell ROUTER Channel on port: %i", app.shell_port)
MinRK
add ssh tunneling to Engine...
r4585
MinRK
add listen_kernel method to IPEngineApp
r6887 app.iopub_port = app._bind_socket(kernel.iopub_socket, app.iopub_port)
app.log.debug("iopub PUB Channel on port: %i", app.iopub_port)
kernel.stdin_socket = self.engine.context.socket(zmq.ROUTER)
app.stdin_port = app._bind_socket(kernel.stdin_socket, app.stdin_port)
app.log.debug("stdin ROUTER Channel on port: %i", app.stdin_port)
# start the heartbeat, and log connection info:
app.init_heartbeat()
app.log_connection_info()
app.write_connection_file()
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 def init_engine(self):
MinRK
Refactor newparallel to use Config system...
r3604 # This is the working dir by now.
sys.path.insert(0, '')
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 config = self.config
# print config
self.find_url_file()
MinRK
allow engines to wait for url_files to arrive...
r4120
# was the url manually specified?
keys = set(self.config.EngineFactory.keys())
keys = keys.union(set(self.config.RegistrationFactory.keys()))
if keys.intersection(set(['ip', 'url', 'port'])):
# Connection info was specified, don't wait for the file
url_specified = True
self.wait_for_url_file = 0
else:
url_specified = False
if self.wait_for_url_file and not os.path.exists(self.url_file):
MinRK
minor cleanup in ipcontroller/ipengine...
r5483 self.log.warn("url_file %r not found", self.url_file)
self.log.warn("Waiting up to %.1f seconds for it to arrive.", self.wait_for_url_file)
MinRK
allow engines to wait for url_files to arrive...
r4120 tic = time.time()
while not os.path.exists(self.url_file) and (time.time()-tic < self.wait_for_url_file):
MinRK
minor cleanup in ipcontroller/ipengine...
r5483 # wait for url_file to exist, or until time limit
MinRK
allow engines to wait for url_files to arrive...
r4120 time.sleep(0.1)
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 if os.path.exists(self.url_file):
MinRK
add ssh tunneling to Engine...
r4585 self.load_connector_file()
MinRK
allow engines to wait for url_files to arrive...
r4120 elif not url_specified:
MinRK
minor cleanup in ipcontroller/ipengine...
r5483 self.log.fatal("Fatal: url file never arrived: %s", self.url_file)
MinRK
allow engines to wait for url_files to arrive...
r4120 self.exit(1)
MinRK
AttributeError check on config no longer works...
r12796 exec_lines = []
for app in ('IPKernelApp', 'InteractiveShellApp'):
MinRK
fix engine startup files...
r16600 if '%s.exec_lines' % app in config:
exec_lines = config[app].exec_lines
MinRK
AttributeError check on config no longer works...
r12796 break
MinRK
persist connection data to disk as json
r3614
MinRK
AttributeError check on config no longer works...
r12796 exec_files = []
for app in ('IPKernelApp', 'InteractiveShellApp'):
MinRK
fix engine startup files...
r16600 if '%s.exec_files' % app in config:
exec_files = config[app].exec_files
MinRK
AttributeError check on config no longer works...
r12796 break
MinRK
persist connection data to disk as json
r3614
MinRK
fix engine startup files...
r16600 config.IPKernelApp.exec_lines = exec_lines
config.IPKernelApp.exec_files = exec_files
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 if self.startup_script:
MinRK
use KernelApp.exec_lines/files in IPEngineApp...
r8062 exec_files.append(self.startup_script)
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 if self.startup_command:
exec_lines.append(self.startup_command)
MinRK
Refactor newparallel to use Config system...
r3604
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 # Create the underlying shell class and Engine
MinRK
Refactor newparallel to use Config system...
r3604 # shell_class = import_item(self.master_config.Global.shell_class)
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 # print self.config
MinRK
Refactor newparallel to use Config system...
r3604 try:
MinRK
simplify IPython.parallel connections...
r7889 self.engine = EngineFactory(config=config, log=self.log,
connection_info=self.connection_info,
)
MinRK
Refactor newparallel to use Config system...
r3604 except:
self.log.error("Couldn't start the Engine", exc_info=True)
self.exit(1)
MinRK
add ssh tunneling to Engine...
r4585
MinRK
re-enable log forwarding and iplogger
r3989 def forward_logging(self):
if self.log_url:
MinRK
minor cleanup in ipcontroller/ipengine...
r5483 self.log.info("Forwarding logging to %s", self.log_url)
MinRK
re-enable log forwarding and iplogger
r3989 context = self.engine.context
lsock = context.socket(zmq.PUB)
lsock.connect(self.log_url)
handler = EnginePUBHandler(self.engine, lsock)
handler.setLevel(self.log_level)
self.log.addHandler(handler)
MinRK
add ssh tunneling to Engine...
r4585
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 def init_mpi(self):
MinRK
Refactor newparallel to use Config system...
r3604 global mpi
MinRK
use `parent=self` throughout IPython...
r11064 self.mpi = MPI(parent=self)
MinRK
all ipcluster scripts in some degree of working order with new config
r3985
mpi_import_statement = self.mpi.init_script
if mpi_import_statement:
MinRK
Refactor newparallel to use Config system...
r3604 try:
self.log.info("Initializing MPI:")
self.log.info(mpi_import_statement)
Thomas Kluyver
Fix exec statements for Py 3...
r13350 exec(mpi_import_statement, globals())
MinRK
Refactor newparallel to use Config system...
r3604 except:
mpi = None
else:
mpi = None
MinRK
catch_config -> catch_config_error
r5214 @catch_config_error
MinRK
ipcluster implemented with new subcommands
r3986 def initialize(self, argv=None):
super(IPEngineApp, self).initialize(argv)
self.init_mpi()
self.init_engine()
MinRK
re-enable log forwarding and iplogger
r3989 self.forward_logging()
MinRK
ipcluster implemented with new subcommands
r3986
MinRK
all ipcluster scripts in some degree of working order with new config
r3985 def start(self):
MinRK
Refactor newparallel to use Config system...
r3604 self.engine.start()
try:
self.engine.loop.start()
except KeyboardInterrupt:
self.log.critical("Engine Interrupted, shutting down...\n")
MinRK
Application.launch_instance...
r11176 launch_new_instance = IPEngineApp.launch_instance
MinRK
Refactor newparallel to use Config system...
r3604
if __name__ == '__main__':
launch_new_instance()