Skip to content

Commit

Permalink
Fix event loop handling (#362)
Browse files Browse the repository at this point in the history
* Fix event loop handling

* update typing
  • Loading branch information
blink1073 authored Sep 27, 2023
1 parent cde7c08 commit 3b353d7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
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:
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

0 comments on commit 3b353d7

Please sign in to comment.