|
1 | 1 | from __future__ import print_function |
2 | 2 | import os |
| 3 | +import sys |
3 | 4 |
|
4 | | -from IPython.kernel.inprocess import InProcessKernelManager |
5 | | -from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell |
| 5 | +from ipykernel.inprocess import InProcessKernelManager |
| 6 | +from jupyter_console.ptshell import ZMQTerminalInteractiveShell |
6 | 7 |
|
7 | 8 |
|
8 | 9 | def print_process_id(): |
9 | 10 | print('Process ID is:', os.getpid()) |
10 | 11 |
|
11 | 12 |
|
| 13 | +def init_asyncio_patch(): |
| 14 | + """set default asyncio policy to be compatible with tornado |
| 15 | + Tornado 6 (at least) is not compatible with the default |
| 16 | + asyncio implementation on Windows |
| 17 | + Pick the older SelectorEventLoopPolicy on Windows |
| 18 | + if the known-incompatible default policy is in use. |
| 19 | + do this as early as possible to make it a low priority and overrideable |
| 20 | + ref: https://github.com/tornadoweb/tornado/issues/2608 |
| 21 | + FIXME: if/when tornado supports the defaults in asyncio, |
| 22 | + remove and bump tornado requirement for py38 |
| 23 | + """ |
| 24 | + if sys.platform.startswith("win") and sys.version_info >= (3, 8): |
| 25 | + import asyncio |
| 26 | + try: |
| 27 | + from asyncio import ( |
| 28 | + WindowsProactorEventLoopPolicy, |
| 29 | + WindowsSelectorEventLoopPolicy, |
| 30 | + ) |
| 31 | + except ImportError: |
| 32 | + pass |
| 33 | + # not affected |
| 34 | + else: |
| 35 | + if type(asyncio.get_event_loop_policy()) is WindowsProactorEventLoopPolicy: |
| 36 | + # WindowsProactorEventLoopPolicy is not compatible with tornado 6 |
| 37 | + # fallback to the pre-3.8 default of Selector |
| 38 | + asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy()) |
| 39 | + |
| 40 | + |
12 | 41 | def main(): |
13 | 42 | print_process_id() |
14 | 43 |
|
15 | 44 | # Create an in-process kernel |
16 | 45 | # >>> print_process_id() |
17 | 46 | # will print the same process ID as the main process |
| 47 | + init_asyncio_patch() |
18 | 48 | kernel_manager = InProcessKernelManager() |
19 | 49 | kernel_manager.start_kernel() |
20 | 50 | kernel = kernel_manager.kernel |
|
0 commit comments