##// END OF EJS Templates
use Context.instance() for default in SessionFactory
MinRK -
Show More
@@ -1,95 +1,99 b''
1 """Base config factories."""
1 """Base config factories."""
2
2
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (C) 2008-2009 The IPython Development Team
4 # Copyright (C) 2008-2009 The IPython Development Team
5 #
5 #
6 # Distributed under the terms of the BSD License. The full license is in
6 # Distributed under the terms of the BSD License. The full license is in
7 # the file COPYING, distributed as part of this software.
7 # the file COPYING, distributed as part of this software.
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Imports
11 # Imports
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14
14
15 import logging
15 import logging
16 import os
16 import os
17
17
18 import zmq
18 from zmq.eventloop.ioloop import IOLoop
19 from zmq.eventloop.ioloop import IOLoop
19
20
20 from IPython.config.configurable import Configurable
21 from IPython.config.configurable import Configurable
21 from IPython.utils.traitlets import Int, Instance, Unicode
22 from IPython.utils.traitlets import Int, Instance, Unicode
22
23
23 import IPython.parallel.streamsession as ss
24 import IPython.parallel.streamsession as ss
24 from IPython.parallel.util import select_random_ports
25 from IPython.parallel.util import select_random_ports
25
26
26 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
27 # Classes
28 # Classes
28 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
29 class LoggingFactory(Configurable):
30 class LoggingFactory(Configurable):
30 """A most basic class, that has a `log` (type:`Logger`) attribute, set via a `logname` Trait."""
31 """A most basic class, that has a `log` (type:`Logger`) attribute, set via a `logname` Trait."""
31 log = Instance('logging.Logger', ('ZMQ', logging.WARN))
32 log = Instance('logging.Logger', ('ZMQ', logging.WARN))
32 logname = Unicode('ZMQ')
33 logname = Unicode('ZMQ')
33 def _logname_changed(self, name, old, new):
34 def _logname_changed(self, name, old, new):
34 self.log = logging.getLogger(new)
35 self.log = logging.getLogger(new)
35
36
36
37
37 class SessionFactory(LoggingFactory):
38 class SessionFactory(LoggingFactory):
38 """The Base factory from which every factory in IPython.parallel inherits"""
39 """The Base factory from which every factory in IPython.parallel inherits"""
39
40
40 # not configurable:
41 # not configurable:
41 context = Instance('zmq.Context', (), {})
42 context = Instance('zmq.Context')
43 def _context_default(self):
44 return zmq.Context.instance()
45
42 session = Instance('IPython.parallel.streamsession.StreamSession')
46 session = Instance('IPython.parallel.streamsession.StreamSession')
43 loop = Instance('zmq.eventloop.ioloop.IOLoop', allow_none=False)
47 loop = Instance('zmq.eventloop.ioloop.IOLoop', allow_none=False)
44 def _loop_default(self):
48 def _loop_default(self):
45 return IOLoop.instance()
49 return IOLoop.instance()
46
50
47
51
48 def __init__(self, **kwargs):
52 def __init__(self, **kwargs):
49 super(SessionFactory, self).__init__(**kwargs)
53 super(SessionFactory, self).__init__(**kwargs)
50
54
51 # construct the session
55 # construct the session
52 self.session = ss.StreamSession(**kwargs)
56 self.session = ss.StreamSession(**kwargs)
53
57
54
58
55 class RegistrationFactory(SessionFactory):
59 class RegistrationFactory(SessionFactory):
56 """The Base Configurable for objects that involve registration."""
60 """The Base Configurable for objects that involve registration."""
57
61
58 url = Unicode('', config=True,
62 url = Unicode('', config=True,
59 help="""The 0MQ url used for registration. This sets transport, ip, and port
63 help="""The 0MQ url used for registration. This sets transport, ip, and port
60 in one variable. For example: url='tcp://127.0.0.1:12345' or
64 in one variable. For example: url='tcp://127.0.0.1:12345' or
61 url='epgm://*:90210'""") # url takes precedence over ip,regport,transport
65 url='epgm://*:90210'""") # url takes precedence over ip,regport,transport
62 transport = Unicode('tcp', config=True,
66 transport = Unicode('tcp', config=True,
63 help="""The 0MQ transport for communications. This will likely be
67 help="""The 0MQ transport for communications. This will likely be
64 the default of 'tcp', but other values include 'ipc', 'epgm', 'inproc'.""")
68 the default of 'tcp', but other values include 'ipc', 'epgm', 'inproc'.""")
65 ip = Unicode('127.0.0.1', config=True,
69 ip = Unicode('127.0.0.1', config=True,
66 help="""The IP address for registration. This is generally either
70 help="""The IP address for registration. This is generally either
67 '127.0.0.1' for loopback only or '*' for all interfaces.
71 '127.0.0.1' for loopback only or '*' for all interfaces.
68 [default: '127.0.0.1']""")
72 [default: '127.0.0.1']""")
69 regport = Int(config=True,
73 regport = Int(config=True,
70 help="""The port on which the Hub listens for registration.""")
74 help="""The port on which the Hub listens for registration.""")
71 def _regport_default(self):
75 def _regport_default(self):
72 return select_random_ports(1)[0]
76 return select_random_ports(1)[0]
73
77
74 def __init__(self, **kwargs):
78 def __init__(self, **kwargs):
75 super(RegistrationFactory, self).__init__(**kwargs)
79 super(RegistrationFactory, self).__init__(**kwargs)
76 self._propagate_url()
80 self._propagate_url()
77 self._rebuild_url()
81 self._rebuild_url()
78 self.on_trait_change(self._propagate_url, 'url')
82 self.on_trait_change(self._propagate_url, 'url')
79 self.on_trait_change(self._rebuild_url, 'ip')
83 self.on_trait_change(self._rebuild_url, 'ip')
80 self.on_trait_change(self._rebuild_url, 'transport')
84 self.on_trait_change(self._rebuild_url, 'transport')
81 self.on_trait_change(self._rebuild_url, 'regport')
85 self.on_trait_change(self._rebuild_url, 'regport')
82
86
83 def _rebuild_url(self):
87 def _rebuild_url(self):
84 self.url = "%s://%s:%i"%(self.transport, self.ip, self.regport)
88 self.url = "%s://%s:%i"%(self.transport, self.ip, self.regport)
85
89
86 def _propagate_url(self):
90 def _propagate_url(self):
87 """Ensure self.url contains full transport://interface:port"""
91 """Ensure self.url contains full transport://interface:port"""
88 if self.url:
92 if self.url:
89 iface = self.url.split('://',1)
93 iface = self.url.split('://',1)
90 if len(iface) == 2:
94 if len(iface) == 2:
91 self.transport,iface = iface
95 self.transport,iface = iface
92 iface = iface.split(':')
96 iface = iface.split(':')
93 self.ip = iface[0]
97 self.ip = iface[0]
94 if iface[1]:
98 if iface[1]:
95 self.regport = int(iface[1])
99 self.regport = int(iface[1])
General Comments 0
You need to be logged in to leave comments. Login now