def test_num_cpus(): with pytest.deprecated_call(): sysinfo.num_cpus()
class IPClusterEngines(BaseParallelApplication): name = u'ipcluster' description = engines_help examples = _engines_examples usage = None default_log_level = logging.INFO classes = List() def _classes_default(self): from IPython.parallel.apps import launcher launchers = launcher.all_launchers eslaunchers = [ l for l in launchers if 'EngineSet' in l.__name__] return [ProfileDir]+eslaunchers n = Integer(num_cpus(), config=True, help="""The number of engines to start. The default is to use one for each CPU on your machine""") engine_launcher = Any(config=True, help="Deprecated, use engine_launcher_class") def _engine_launcher_changed(self, name, old, new): if isinstance(new, string_types): self.log.warn("WARNING: %s.engine_launcher is deprecated as of 0.12," " use engine_launcher_class" % self.__class__.__name__) self.engine_launcher_class = new engine_launcher_class = DottedObjectName('LocalEngineSetLauncher', config=True, help="""The class for launching a set of Engines. Change this value to use various batch systems to launch your engines, such as PBS,SGE,MPI,etc. Each launcher class has its own set of configuration options, for making sure it will work in your environment. You can also write your own launcher, and specify it's absolute import path, as in 'mymodule.launcher.FTLEnginesLauncher`. IPython's bundled examples include: Local : start engines locally as subprocesses [default] MPI : use mpiexec to launch engines in an MPI environment PBS : use PBS (qsub) to submit engines to a batch queue SGE : use SGE (qsub) to submit engines to a batch queue LSF : use LSF (bsub) to submit engines to a batch queue SSH : use SSH to start the controller Note that SSH does *not* move the connection files around, so you will likely have to do this manually unless the machines are on a shared file system. HTCondor : use HTCondor to submit engines to a batch queue WindowsHPC : use Windows HPC If you are using one of IPython's builtin launchers, you can specify just the prefix, e.g: c.IPClusterEngines.engine_launcher_class = 'SSH' or: ipcluster start --engines=MPI """ ) daemonize = Bool(False, config=True, help="""Daemonize the ipcluster program. This implies --log-to-file. Not available on Windows. """) def _daemonize_changed(self, name, old, new): if new: self.log_to_file = True early_shutdown = Integer(30, config=True, help="The timeout (in seconds)") _stopping = False aliases = Dict(engine_aliases) flags = Dict(engine_flags) @catch_config_error def initialize(self, argv=None): super(IPClusterEngines, self).initialize(argv) self.init_signal() self.init_launchers() def init_launchers(self): self.engine_launcher = self.build_launcher(self.engine_launcher_class, 'EngineSet') def init_signal(self): # Setup signals signal.signal(signal.SIGINT, self.sigint_handler) def build_launcher(self, clsname, kind=None): """import and instantiate a Launcher based on importstring""" try: klass = find_launcher_class(clsname, kind) except (ImportError, KeyError): self.log.fatal("Could not import launcher class: %r"%clsname) self.exit(1) launcher = klass( work_dir=u'.', parent=self, log=self.log, profile_dir=self.profile_dir.location, cluster_id=self.cluster_id, ) return launcher def engines_started_ok(self): self.log.info("Engines appear to have started successfully") self.early_shutdown = 0 def start_engines(self): # Some EngineSetLaunchers ignore `n` and use their own engine count, such as SSH: n = getattr(self.engine_launcher, 'engine_count', self.n) self.log.info("Starting %s Engines with %s", n, self.engine_launcher_class) try: self.engine_launcher.start(self.n) except: self.log.exception("Engine start failed") raise self.engine_launcher.on_stop(self.engines_stopped_early) if self.early_shutdown: ioloop.DelayedCallback(self.engines_started_ok, self.early_shutdown*1000, self.loop).start() def engines_stopped_early(self, r): if self.early_shutdown and not self._stopping: self.log.error(""" Engines shutdown early, they probably failed to connect. Check the engine log files for output. If your controller and engines are not on the same machine, you probably have to instruct the controller to listen on an interface other than localhost. You can set this by adding "--ip='*'" to your ControllerLauncher.controller_args. Be sure to read our security docs before instructing your controller to listen on a public interface. """) self.stop_launchers() return self.engines_stopped(r) def engines_stopped(self, r): return self.loop.stop() def stop_engines(self): if self.engine_launcher.running: self.log.info("Stopping Engines...") d = self.engine_launcher.stop() return d else: return None def stop_launchers(self, r=None): if not self._stopping: self._stopping = True self.log.error("IPython cluster: stopping") self.stop_engines() # Wait a few seconds to let things shut down. dc = ioloop.DelayedCallback(self.loop.stop, 3000, self.loop) dc.start() def sigint_handler(self, signum, frame): self.log.debug("SIGINT received, stopping launchers...") self.stop_launchers() def start_logging(self): # Remove old log files of the controller and engine if self.clean_logs: log_dir = self.profile_dir.log_dir for f in os.listdir(log_dir): if re.match(r'ip(engine|controller)-.+\.(log|err|out)',f): os.remove(os.path.join(log_dir, f)) def start(self): """Start the app for the engines subcommand.""" self.log.info("IPython cluster: started") # First see if the cluster is already running # Now log and daemonize self.log.info( 'Starting engines with [daemon=%r]' % self.daemonize ) # TODO: Get daemonize working on Windows or as a Windows Server. if self.daemonize: if os.name=='posix': daemonize() dc = ioloop.DelayedCallback(self.start_engines, 0, self.loop) dc.start() # Now write the new pid file AFTER our new forked pid is active. # self.write_pid_file() try: self.loop.start() except KeyboardInterrupt: pass except zmq.ZMQError as e: if e.errno == errno.EINTR: pass else: raise
class IPClusterEngines(BaseParallelApplication): name = u'ipcluster' description = engines_help examples = _engines_examples usage = None config_file_name = Unicode(default_config_file_name) default_log_level = logging.INFO classes = List() def _classes_default(self): from IPython.parallel.apps import launcher launchers = launcher.all_launchers eslaunchers = [l for l in launchers if 'EngineSet' in l.__name__] return [ProfileDir] + eslaunchers n = Int( num_cpus(), config=True, help= """The number of engines to start. The default is to use one for each CPU on your machine""") engine_launcher_class = DottedObjectName( 'LocalEngineSetLauncher', config=True, help="""The class for launching a set of Engines. Change this value to use various batch systems to launch your engines, such as PBS,SGE,MPIExec,etc. Each launcher class has its own set of configuration options, for making sure it will work in your environment. You can also write your own launcher, and specify it's absolute import path, as in 'mymodule.launcher.FTLEnginesLauncher`. Examples include: LocalEngineSetLauncher : start engines locally as subprocesses [default] MPIExecEngineSetLauncher : use mpiexec to launch in an MPI environment PBSEngineSetLauncher : use PBS (qsub) to submit engines to a batch queue SGEEngineSetLauncher : use SGE (qsub) to submit engines to a batch queue SSHEngineSetLauncher : use SSH to start the controller Note that SSH does *not* move the connection files around, so you will likely have to do this manually unless the machines are on a shared file system. WindowsHPCEngineSetLauncher : use Windows HPC """) daemonize = Bool( False, config=True, help="""Daemonize the ipcluster program. This implies --log-to-file. Not available on Windows. """) def _daemonize_changed(self, name, old, new): if new: self.log_to_file = True aliases = Dict(engine_aliases) flags = Dict(engine_flags) _stopping = False def initialize(self, argv=None): super(IPClusterEngines, self).initialize(argv) self.init_signal() self.init_launchers() def init_launchers(self): self.engine_launcher = self.build_launcher(self.engine_launcher_class) self.engine_launcher.on_stop(lambda r: self.loop.stop()) def init_signal(self): # Setup signals signal.signal(signal.SIGINT, self.sigint_handler) def build_launcher(self, clsname): """import and instantiate a Launcher based on importstring""" if '.' not in clsname: # not a module, presume it's the raw name in apps.launcher clsname = 'IPython.parallel.apps.launcher.' + clsname # print repr(clsname) try: klass = import_item(clsname) except (ImportError, KeyError): self.log.fatal("Could not import launcher class: %r" % clsname) self.exit(1) launcher = klass(work_dir=u'.', config=self.config, log=self.log) return launcher def start_engines(self): self.log.info("Starting %i engines" % self.n) self.engine_launcher.start(self.n, self.profile_dir.location) def stop_engines(self): self.log.info("Stopping Engines...") if self.engine_launcher.running: d = self.engine_launcher.stop() return d else: return None def stop_launchers(self, r=None): if not self._stopping: self._stopping = True self.log.error("IPython cluster: stopping") self.stop_engines() # Wait a few seconds to let things shut down. dc = ioloop.DelayedCallback(self.loop.stop, 4000, self.loop) dc.start() def sigint_handler(self, signum, frame): self.log.debug("SIGINT received, stopping launchers...") self.stop_launchers() def start_logging(self): # Remove old log files of the controller and engine if self.clean_logs: log_dir = self.profile_dir.log_dir for f in os.listdir(log_dir): if re.match(r'ip(engine|controller)z-\d+\.(log|err|out)', f): os.remove(os.path.join(log_dir, f)) # This will remove old log files for ipcluster itself # super(IPBaseParallelApplication, self).start_logging() def start(self): """Start the app for the engines subcommand.""" self.log.info("IPython cluster: started") # First see if the cluster is already running # Now log and daemonize self.log.info('Starting engines with [daemon=%r]' % self.daemonize) # TODO: Get daemonize working on Windows or as a Windows Server. if self.daemonize: if os.name == 'posix': daemonize() dc = ioloop.DelayedCallback(self.start_engines, 0, self.loop) dc.start() # Now write the new pid file AFTER our new forked pid is active. # self.write_pid_file() try: self.loop.start() except KeyboardInterrupt: pass except zmq.ZMQError as e: if e.errno == errno.EINTR: pass else: raise