Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix event loop handling #362

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions jupyter_core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _log_level_default(self) -> int:
def _jupyter_path_default(self) -> list[str]:
return jupyter_path()

config_dir: str | Unicode = Unicode()
config_dir = Unicode()

def _config_dir_default(self) -> str:
return jupyter_config_dir()
Expand All @@ -96,41 +96,39 @@ def config_file_paths(self) -> list[str]:
path.insert(0, self.config_dir)
return path

data_dir: str | Unicode = Unicode()
data_dir = Unicode()

def _data_dir_default(self) -> str:
d = jupyter_data_dir()
ensure_dir_exists(d, mode=0o700)
return d

runtime_dir: str | Unicode = Unicode()
runtime_dir = Unicode()

def _runtime_dir_default(self) -> str:
rd = jupyter_runtime_dir()
ensure_dir_exists(rd, mode=0o700)
return rd

@observe("runtime_dir") # type:ignore[misc]
@observe("runtime_dir")
def _runtime_dir_changed(self, change: t.Any) -> None:
ensure_dir_exists(change["new"], mode=0o700)

generate_config: bool | Bool = Bool(
False, config=True, help="""Generate default config file."""
)
generate_config = Bool(False, config=True, help="""Generate default config file.""")

config_file_name: str | Unicode = Unicode(config=True, help="Specify a config file to load.")
config_file_name = Unicode(config=True, help="Specify a config file to load.")

def _config_file_name_default(self) -> str:
if not self.name:
return ""
return self.name.replace("-", "_") + "_config"

config_file: str | Unicode = Unicode(
config_file = Unicode(
config=True,
help="""Full path of a config file.""",
)

answer_yes: bool | Bool = Bool(False, config=True, help="""Answer yes to any prompts.""")
answer_yes = Bool(False, config=True, help="""Answer yes to any prompts.""")

def write_default_config(self) -> None:
"""Write our default config to a .py config file"""
Expand Down
9 changes: 5 additions & 4 deletions jupyter_core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ def run(self, coro: Any) -> Any:


_runner_map: dict[str, _TaskRunner] = {}
_loop_map: dict[str, asyncio.AbstractEventLoop] = {}


def run_sync(coro: Callable[..., Awaitable[T]]) -> Callable[..., T]:
Expand Down Expand Up @@ -161,9 +160,11 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
pass

# Run the loop for this thread.
if name not in _loop_map:
_loop_map[name] = asyncio.new_event_loop()
loop = _loop_map[name]
try:
loop = asyncio.get_event_loop()
except RuntimeError:
Comment on lines +163 to +165
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you actually want loop = asyncio.get_running_loop() here?

According to https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop this is deprecated in python 3.12 and will emit https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop. This causes some test failures (e.g. in jupyter_client).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I suggested above does fix the test failures in jupyter_client I was getting, at the expense of causing other test failures in jupyter_client.

The only thing that makes the jupyter_client testsuite pass (on python 3.12) is reverting the current PR. I'll downgrade to jupyter_core 5.3.1 for the time being.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up @tornaria, I opened #367

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(inner)

wrapped.__doc__ = coro.__doc__
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ nowarn = "test -W default {args}"

[tool.hatch.envs.typing]
features = ["test"]
dependencies = ["mypy>=1.5.1"]
dependencies = ["mypy>=1.5.1", "traitlets>=5.10.1"]
[tool.hatch.envs.typing.scripts]
test = "mypy --install-types --non-interactive {args}"

Expand Down
Loading