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