Show More
@@ -1,172 +1,172 | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 |
|
3 | |||
4 | """Start the IPython Engine.""" |
|
4 | """Start the IPython Engine.""" | |
5 |
|
5 | |||
6 | __docformat__ = "restructuredtext en" |
|
6 | __docformat__ = "restructuredtext en" | |
7 |
|
7 | |||
8 | #------------------------------------------------------------------------------- |
|
8 | #------------------------------------------------------------------------------- | |
9 | # Copyright (C) 2008 The IPython Development Team |
|
9 | # Copyright (C) 2008 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #------------------------------------------------------------------------------- |
|
13 | #------------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #------------------------------------------------------------------------------- |
|
15 | #------------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #------------------------------------------------------------------------------- |
|
17 | #------------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | # Python looks for an empty string at the beginning of sys.path to enable |
|
19 | # Python looks for an empty string at the beginning of sys.path to enable | |
20 | # importing from the cwd. |
|
20 | # importing from the cwd. | |
21 | import sys |
|
21 | import sys | |
22 | sys.path.insert(0, '') |
|
22 | sys.path.insert(0, '') | |
23 |
|
23 | |||
24 | import sys, os |
|
24 | import sys, os | |
25 | from optparse import OptionParser |
|
25 | from optparse import OptionParser | |
26 |
|
26 | |||
27 | from twisted.application import service |
|
27 | from twisted.application import service | |
28 | from twisted.internet import reactor |
|
28 | from twisted.internet import reactor | |
29 | from twisted.python import log |
|
29 | from twisted.python import log | |
30 |
|
30 | |||
31 | from IPython.kernel.fcutil import Tub, UnauthenticatedTub |
|
31 | from IPython.kernel.fcutil import Tub, UnauthenticatedTub | |
32 |
|
32 | |||
33 | from IPython.kernel.core.config import config_manager as core_config_manager |
|
33 | from IPython.kernel.core.config import config_manager as core_config_manager | |
34 | from IPython.config.cutils import import_item |
|
34 | from IPython.config.cutils import import_item | |
35 | from IPython.kernel.engineservice import EngineService |
|
35 | from IPython.kernel.engineservice import EngineService | |
36 | from IPython.kernel.config import config_manager as kernel_config_manager |
|
36 | from IPython.kernel.config import config_manager as kernel_config_manager | |
37 | from IPython.kernel.engineconnector import EngineConnector |
|
37 | from IPython.kernel.engineconnector import EngineConnector | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | #------------------------------------------------------------------------------- |
|
40 | #------------------------------------------------------------------------------- | |
41 | # Code |
|
41 | # Code | |
42 | #------------------------------------------------------------------------------- |
|
42 | #------------------------------------------------------------------------------- | |
43 |
|
43 | |||
44 | def start_engine(): |
|
44 | def start_engine(): | |
45 | """ |
|
45 | """ | |
46 | Start the engine, by creating it and starting the Twisted reactor. |
|
46 | Start the engine, by creating it and starting the Twisted reactor. | |
47 |
|
47 | |||
48 | This method does: |
|
48 | This method does: | |
49 |
|
49 | |||
50 | * If it exists, runs the `mpi_import_statement` to call `MPI_Init` |
|
50 | * If it exists, runs the `mpi_import_statement` to call `MPI_Init` | |
51 | * Starts the engine logging |
|
51 | * Starts the engine logging | |
52 | * Creates an IPython shell and wraps it in an `EngineService` |
|
52 | * Creates an IPython shell and wraps it in an `EngineService` | |
53 | * Creates a `foolscap.Tub` to use in connecting to a controller. |
|
53 | * Creates a `foolscap.Tub` to use in connecting to a controller. | |
54 | * Uses the tub and the `EngineService` along with a Foolscap URL |
|
54 | * Uses the tub and the `EngineService` along with a Foolscap URL | |
55 | (or FURL) to connect to the controller and register the engine |
|
55 | (or FURL) to connect to the controller and register the engine | |
56 | with the controller |
|
56 | with the controller | |
57 | """ |
|
57 | """ | |
58 | kernel_config = kernel_config_manager.get_config_obj() |
|
58 | kernel_config = kernel_config_manager.get_config_obj() | |
59 | core_config = core_config_manager.get_config_obj() |
|
59 | core_config = core_config_manager.get_config_obj() | |
60 |
|
60 | |||
61 |
|
61 | |||
62 | # Execute the mpi import statement that needs to call MPI_Init |
|
62 | # Execute the mpi import statement that needs to call MPI_Init | |
63 | global mpi |
|
63 | global mpi | |
64 | mpikey = kernel_config['mpi']['default'] |
|
64 | mpikey = kernel_config['mpi']['default'] | |
65 | mpi_import_statement = kernel_config['mpi'].get(mpikey, None) |
|
65 | mpi_import_statement = kernel_config['mpi'].get(mpikey, None) | |
66 | if mpi_import_statement is not None: |
|
66 | if mpi_import_statement is not None: | |
67 | try: |
|
67 | try: | |
68 | exec mpi_import_statement in globals() |
|
68 | exec mpi_import_statement in globals() | |
69 | except: |
|
69 | except: | |
70 | mpi = None |
|
70 | mpi = None | |
71 | else: |
|
71 | else: | |
72 | mpi = None |
|
72 | mpi = None | |
73 |
|
73 | |||
74 | # Start logging |
|
74 | # Start logging | |
75 | logfile = kernel_config['engine']['logfile'] |
|
75 | logfile = kernel_config['engine']['logfile'] | |
76 | if logfile: |
|
76 | if logfile: | |
77 | logfile = logfile + str(os.getpid()) + '.log' |
|
77 | logfile = logfile + str(os.getpid()) + '.log' | |
78 | try: |
|
78 | try: | |
79 | openLogFile = open(logfile, 'w') |
|
79 | openLogFile = open(logfile, 'w') | |
80 | except: |
|
80 | except: | |
81 | openLogFile = sys.stdout |
|
81 | openLogFile = sys.stdout | |
82 | else: |
|
82 | else: | |
83 | openLogFile = sys.stdout |
|
83 | openLogFile = sys.stdout | |
84 | log.startLogging(openLogFile) |
|
84 | log.startLogging(openLogFile) | |
85 |
|
85 | |||
86 | # Create the underlying shell class and EngineService |
|
86 | # Create the underlying shell class and EngineService | |
87 | shell_class = import_item(core_config['shell']['shell_class']) |
|
87 | shell_class = import_item(core_config['shell']['shell_class']) | |
88 | engine_service = EngineService(shell_class, mpi=mpi) |
|
88 | engine_service = EngineService(shell_class, mpi=mpi) | |
89 | shell_import_statement = core_config['shell']['import_statement'] |
|
89 | shell_import_statement = core_config['shell']['import_statement'] | |
90 | if shell_import_statement: |
|
90 | if shell_import_statement: | |
91 | try: |
|
91 | try: | |
92 | engine_service.execute(shell_import_statement) |
|
92 | engine_service.execute(shell_import_statement) | |
93 | except: |
|
93 | except: | |
94 | log.msg("Error running import_statement: %s" % sis) |
|
94 | log.msg("Error running import_statement: %s" % shell_import_statement) | |
95 |
|
95 | |||
96 | # Create the service hierarchy |
|
96 | # Create the service hierarchy | |
97 | main_service = service.MultiService() |
|
97 | main_service = service.MultiService() | |
98 | engine_service.setServiceParent(main_service) |
|
98 | engine_service.setServiceParent(main_service) | |
99 | tub_service = Tub() |
|
99 | tub_service = Tub() | |
100 | tub_service.setServiceParent(main_service) |
|
100 | tub_service.setServiceParent(main_service) | |
101 | # This needs to be called before the connection is initiated |
|
101 | # This needs to be called before the connection is initiated | |
102 | main_service.startService() |
|
102 | main_service.startService() | |
103 |
|
103 | |||
104 | # This initiates the connection to the controller and calls |
|
104 | # This initiates the connection to the controller and calls | |
105 | # register_engine to tell the controller we are ready to do work |
|
105 | # register_engine to tell the controller we are ready to do work | |
106 | engine_connector = EngineConnector(tub_service) |
|
106 | engine_connector = EngineConnector(tub_service) | |
107 | furl_file = kernel_config['engine']['furl_file'] |
|
107 | furl_file = kernel_config['engine']['furl_file'] | |
108 | log.msg("Using furl file: %s" % furl_file) |
|
108 | log.msg("Using furl file: %s" % furl_file) | |
109 | d = engine_connector.connect_to_controller(engine_service, furl_file) |
|
109 | d = engine_connector.connect_to_controller(engine_service, furl_file) | |
110 | d.addErrback(lambda _: reactor.stop()) |
|
110 | d.addErrback(lambda _: reactor.stop()) | |
111 |
|
111 | |||
112 | reactor.run() |
|
112 | reactor.run() | |
113 |
|
113 | |||
114 |
|
114 | |||
115 | def init_config(): |
|
115 | def init_config(): | |
116 | """ |
|
116 | """ | |
117 | Initialize the configuration using default and command line options. |
|
117 | Initialize the configuration using default and command line options. | |
118 | """ |
|
118 | """ | |
119 |
|
119 | |||
120 | parser = OptionParser() |
|
120 | parser = OptionParser() | |
121 |
|
121 | |||
122 | parser.add_option( |
|
122 | parser.add_option( | |
123 | "--furl-file", |
|
123 | "--furl-file", | |
124 | type="string", |
|
124 | type="string", | |
125 | dest="furl_file", |
|
125 | dest="furl_file", | |
126 | help="The filename containing the FURL of the controller" |
|
126 | help="The filename containing the FURL of the controller" | |
127 | ) |
|
127 | ) | |
128 | parser.add_option( |
|
128 | parser.add_option( | |
129 | "--mpi", |
|
129 | "--mpi", | |
130 | type="string", |
|
130 | type="string", | |
131 | dest="mpi", |
|
131 | dest="mpi", | |
132 | help="How to enable MPI (mpi4py, pytrilinos, or empty string to disable)" |
|
132 | help="How to enable MPI (mpi4py, pytrilinos, or empty string to disable)" | |
133 | ) |
|
133 | ) | |
134 | parser.add_option( |
|
134 | parser.add_option( | |
135 | "-l", |
|
135 | "-l", | |
136 | "--logfile", |
|
136 | "--logfile", | |
137 | type="string", |
|
137 | type="string", | |
138 | dest="logfile", |
|
138 | dest="logfile", | |
139 | help="log file name (default is stdout)" |
|
139 | help="log file name (default is stdout)" | |
140 | ) |
|
140 | ) | |
141 | parser.add_option( |
|
141 | parser.add_option( | |
142 | "--ipythondir", |
|
142 | "--ipythondir", | |
143 | type="string", |
|
143 | type="string", | |
144 | dest="ipythondir", |
|
144 | dest="ipythondir", | |
145 | help="look for config files and profiles in this directory" |
|
145 | help="look for config files and profiles in this directory" | |
146 | ) |
|
146 | ) | |
147 |
|
147 | |||
148 | (options, args) = parser.parse_args() |
|
148 | (options, args) = parser.parse_args() | |
149 |
|
149 | |||
150 | kernel_config_manager.update_config_obj_from_default_file(options.ipythondir) |
|
150 | kernel_config_manager.update_config_obj_from_default_file(options.ipythondir) | |
151 | core_config_manager.update_config_obj_from_default_file(options.ipythondir) |
|
151 | core_config_manager.update_config_obj_from_default_file(options.ipythondir) | |
152 |
|
152 | |||
153 | kernel_config = kernel_config_manager.get_config_obj() |
|
153 | kernel_config = kernel_config_manager.get_config_obj() | |
154 | # Now override with command line options |
|
154 | # Now override with command line options | |
155 | if options.furl_file is not None: |
|
155 | if options.furl_file is not None: | |
156 | kernel_config['engine']['furl_file'] = options.furl_file |
|
156 | kernel_config['engine']['furl_file'] = options.furl_file | |
157 | if options.logfile is not None: |
|
157 | if options.logfile is not None: | |
158 | kernel_config['engine']['logfile'] = options.logfile |
|
158 | kernel_config['engine']['logfile'] = options.logfile | |
159 | if options.mpi is not None: |
|
159 | if options.mpi is not None: | |
160 | kernel_config['mpi']['default'] = options.mpi |
|
160 | kernel_config['mpi']['default'] = options.mpi | |
161 |
|
161 | |||
162 |
|
162 | |||
163 | def main(): |
|
163 | def main(): | |
164 | """ |
|
164 | """ | |
165 | After creating the configuration information, start the engine. |
|
165 | After creating the configuration information, start the engine. | |
166 | """ |
|
166 | """ | |
167 | init_config() |
|
167 | init_config() | |
168 | start_engine() |
|
168 | start_engine() | |
169 |
|
169 | |||
170 |
|
170 | |||
171 | if __name__ == "__main__": |
|
171 | if __name__ == "__main__": | |
172 | main() |
|
172 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now