Repository: ipython/ipython

Issue:
Problem with Interactive monitoring of a parallel MPI simulation
I got the the first notebook here 

[Interactive monitoring of a parallel MPI simulation with the IPython Notebook](http://nbviewer.ipython.org/github/ipython/ipython/blob/2.x/examples/Parallel%20Computing/Monitoring%20an%20MPI%20Simulation%20-%201.ipynb) working without MPI on a plain Windows 7 workstation.

At the end of the notebook, there is the following: 

```
 %%px --target 0
 from IPython.parallel import bind_kernel; bind_kernel()
 %connect_info
```

But this gives the following error:

```
 ---------------------------------------------------------------------------
 RuntimeError                              Traceback (most recent call last)<ipython-input-3-5e39de44d31d> in <module>()
 ----> 1 from IPython.parallel import bind_kernel; bind_kernel()
       2 get_ipython().magic(u'connect_info')
 c:\Python27\lib\site-packages\IPython\parallel\__init__.pyc in bind_kernel(**kwargs)
      67             return app.bind_kernel(**kwargs)
      68 
 ---> 69     raise RuntimeError("bind_kernel be called from an IPEngineApp instance")
      70 
      71 
 RuntimeError: bind_kernel be called from an IPEngineApp instance
```

Note that I'm using IPython version 2.1.0 on windows 7.



Discussion:
What do you get from:

``` python
from IPython.config.application import Application
from from IPython.parallel.apps.ipengineapp import IPEngineApp

print("Application", Application.initialized(), Application.instance())
print("Engine", IPEngineApp.initialized(), IPEngineApp.instance())
```

in the engine, that is.

```
 ---------------------------------------------------------------------------
 MultipleInstanceError                     Traceback (most recent call last)<ipython-input-4-58ed49483c34> in <module>()
       4 
       5 print("Application", Application.initialized(), Application.instance())
 ----> 6 print("Engine", IPEngineApp.initialized(), IPEngineApp.instance())
 c:\Python27\lib\site-packages\IPython\config\configurable.pyc in instance(cls, *args, **kwargs)
     363             raise MultipleInstanceError(
     364                 'Multiple incompatible subclass instances of '
 --> 365                 '%s are being created.' % cls.__name__
     366             )
     367 
 MultipleInstanceError: Multiple incompatible subclass instances of IPEngineApp are being created.
```

And the output of Application line, before the IPEngineApp line?

How exactly are you starting the engines?

Sorry. Here you go:

```
('Application', True, <__main__.IPEngineApp object at 0x0383DD70>)
```

I started this in a separate DOS cmd window with version Python 2.7.6:

```
c:\>ipcluster start -n 2
2014-12-12 11:12:52.335 [IPClusterStart] Using existing profile dir: u'C:\\Users\\jhunpingco\\.ipython\\profile_default'
2014-12-12 11:12:52.362 [IPClusterStart] Removing pid file: C:\Users\jhunpingco\.ipython\profile_default\pid\ipcluster.pid
2014-12-12 11:12:52.365 [IPClusterStart] Starting ipcluster with [daemon=False]
2014-12-12 11:12:52.365 [IPClusterStart] Creating pid file: C:\Users\jhunpingco\.ipython\profile_default\pid\ipcluster.pid
2014-12-12 11:12:52.367 [IPClusterStart] Starting Controller with LocalControllerLauncher
2014-12-12 11:12:53.365 [IPClusterStart] Starting 2 Engines with LocalEngineSetLauncher
2014-12-12 11:13:23.484 [IPClusterStart] Engines appear to have started successfully
```

What Python version <del>and what command did you use to start the engines</del>?

Python 2.7.6

What is `sys.argv` on the engine?

```
%%px --target 0
import sys
print sys.argv
```

gives

```
['c:\\Python27\\lib\\site-packages\\IPython\\parallel\\apps\\ipengineapp.py', '--profile-dir', 'C:\\Users\\jhunpingco\\.ipython\\profile_default', '--cluster-id', '', '--log-to-file', '--log-level=20']
```

That doesn't seem right, can you share your ipcluster_config.py?

I don't have one. Maybe that's the problem?


Resolving patch:
diff --git a/IPython/parallel/apps/launcher.py b/IPython/parallel/apps/launcher.py
--- a/IPython/parallel/apps/launcher.py
+++ b/IPython/parallel/apps/launcher.py
@@ -68,10 +68,12 @@ def check_output(*args, **kwargs):
 ipcontroller_cmd_argv = [sys.executable, "-m", "IPython.parallel.controller"]
 
 if WINDOWS and sys.version_info < (3,):
-    # `python -m package` doesn't work on Windows Python 2,
-    # but `python -m module` does.
-    ipengine_cmd_argv = [sys.executable, "-m", "IPython.parallel.apps.ipengineapp"]
-    ipcontroller_cmd_argv = [sys.executable, "-m", "IPython.parallel.apps.ipcontrollerapp"]
+    # `python -m package` doesn't work on Windows Python 2
+    # due to weird multiprocessing bugs
+    # and python -m module puts classes in the `__main__` module,
+    # so instance checks get confused
+    ipengine_cmd_argv = [sys.executable, "-c", "from IPython.parallel.engine.__main__ import main; main()"]
+    ipcontroller_cmd_argv = [sys.executable, "-c", "from IPython.parallel.controller.__main__ import main; main()"]
 
 #-----------------------------------------------------------------------------
 # Base launchers and errors
diff --git a/IPython/parallel/controller/__main__.py b/IPython/parallel/controller/__main__.py
--- a/IPython/parallel/controller/__main__.py
+++ b/IPython/parallel/controller/__main__.py
@@ -1,3 +1,6 @@
-if __name__ == '__main__':
+def main():
     from IPython.parallel.apps import ipcontrollerapp as app
     app.launch_new_instance()
+    
+if __name__ == '__main__':
+    main()
diff --git a/IPython/parallel/engine/__main__.py b/IPython/parallel/engine/__main__.py
--- a/IPython/parallel/engine/__main__.py
+++ b/IPython/parallel/engine/__main__.py
@@ -1,3 +1,6 @@
-if __name__ == '__main__':
+def main():
     from IPython.parallel.apps import ipengineapp as app
     app.launch_new_instance()
+    
+if __name__ == '__main__':
+    main()


Test patch: