/IPython/parallel/factory.py

https://github.com/cboos/ipython
Python | 77 lines | 43 code | 14 blank | 20 comment | 3 complexity | 304c9c79fb54cba9912619c48d7a08b0 MD5 | raw file
  1. """Base config factories.
  2. Authors:
  3. * Min RK
  4. """
  5. #-----------------------------------------------------------------------------
  6. # Copyright (C) 2010-2011 The IPython Development Team
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. import logging
  15. import os
  16. import zmq
  17. from zmq.eventloop.ioloop import IOLoop
  18. from IPython.config.configurable import Configurable
  19. from IPython.utils.traitlets import Integer, Instance, Unicode
  20. from IPython.parallel.util import select_random_ports
  21. from IPython.zmq.session import Session, SessionFactory
  22. #-----------------------------------------------------------------------------
  23. # Classes
  24. #-----------------------------------------------------------------------------
  25. class RegistrationFactory(SessionFactory):
  26. """The Base Configurable for objects that involve registration."""
  27. url = Unicode('', config=True,
  28. help="""The 0MQ url used for registration. This sets transport, ip, and port
  29. in one variable. For example: url='tcp://127.0.0.1:12345' or
  30. url='epgm://*:90210'""") # url takes precedence over ip,regport,transport
  31. transport = Unicode('tcp', config=True,
  32. help="""The 0MQ transport for communications. This will likely be
  33. the default of 'tcp', but other values include 'ipc', 'epgm', 'inproc'.""")
  34. ip = Unicode('127.0.0.1', config=True,
  35. help="""The IP address for registration. This is generally either
  36. '127.0.0.1' for loopback only or '*' for all interfaces.
  37. [default: '127.0.0.1']""")
  38. regport = Integer(config=True,
  39. help="""The port on which the Hub listens for registration.""")
  40. def _regport_default(self):
  41. return select_random_ports(1)[0]
  42. def __init__(self, **kwargs):
  43. super(RegistrationFactory, self).__init__(**kwargs)
  44. self._propagate_url()
  45. self._rebuild_url()
  46. self.on_trait_change(self._propagate_url, 'url')
  47. self.on_trait_change(self._rebuild_url, 'ip')
  48. self.on_trait_change(self._rebuild_url, 'transport')
  49. self.on_trait_change(self._rebuild_url, 'regport')
  50. def _rebuild_url(self):
  51. self.url = "%s://%s:%i"%(self.transport, self.ip, self.regport)
  52. def _propagate_url(self):
  53. """Ensure self.url contains full transport://interface:port"""
  54. if self.url:
  55. iface = self.url.split('://',1)
  56. if len(iface) == 2:
  57. self.transport,iface = iface
  58. iface = iface.split(':')
  59. self.ip = iface[0]
  60. if iface[1]:
  61. self.regport = int(iface[1])