Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 4, 2023
1 parent 6906abd commit 960ff5a
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 48 deletions.
10 changes: 3 additions & 7 deletions ipykernel/athread.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import asyncio
import inspect
import threading

import janus


class AThread(threading.Thread):
"""A thread that can run async tasks.
"""
"""A thread that can run async tasks."""

def __init__(self, name, awaitables=[]):
super().__init__(name=name, daemon=True)
Expand Down Expand Up @@ -40,17 +38,15 @@ async def _main(self):
task.cancel()

def create_task(self, awaitable):
"""Create a task in the thread (thread-safe).
"""
"""Create a task in the thread (thread-safe)."""
with self._lock:
if self.__initialized:
self._queue.sync_q.put(awaitable)
else:
self._aws.append(awaitable)

def stop(self):
"""Stop the thread (thread-safe).
"""
"""Stop the thread (thread-safe)."""
with self._lock:
if self.__initialized:
self._queue.sync_q.put(None)
Expand Down
2 changes: 1 addition & 1 deletion ipykernel/inprocess/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class InProcessKernel(IPythonKernel):
_underlying_iopub_socket = Instance(DummySocket, ())
iopub_thread: IOPubThread = Instance(IOPubThread) # type:ignore[assignment]

#shell_stream = Instance(DummySocket, ())
# shell_stream = Instance(DummySocket, ())

@default("iopub_thread")
def _default_iopub_thread(self):
Expand Down
15 changes: 7 additions & 8 deletions ipykernel/iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, socket, pipe=False):
self._events: Deque[Callable[..., Any]] = deque()
self._event_pipes: WeakSet[Any] = WeakSet()
self._setup_event_pipe()
#self.thread = threading.Thread(target=self._thread_main, name="IOPub")
# self.thread = threading.Thread(target=self._thread_main, name="IOPub")
aws = [self._handle_event()]
if self._pipe_flag:
aws.append(self._handle_pipe_msg())
Expand Down Expand Up @@ -169,7 +169,7 @@ def _check_mp_mode(self):

def start(self):
"""Start the IOPub thread"""
#self.thread.name = "IOPub"
# self.thread.name = "IOPub"
self.thread.start()
# make sure we don't prevent process exit
# I'm not sure why setting daemon=True above isn't enough, but it doesn't appear to be.
Expand All @@ -179,7 +179,7 @@ def stop(self):
"""Stop the IOPub thread"""
if not self.thread.is_alive():
return
#self.io_loop.call_soon_threadsafe(self.io_loop.stop)
# self.io_loop.call_soon_threadsafe(self.io_loop.stop)
self.thread.stop()
self.thread.join()
# close *all* event pipes, created in any thread
Expand Down Expand Up @@ -210,9 +210,9 @@ def schedule(self, f):
if self.thread.is_alive():
self._events.append(f)
# wake event thread (message content is ignored)
#try:
# try:
# asyncio.get_running_loop()
#except BaseException:
# except BaseException:
# asyncio.set_event_loop(asyncio.new_event_loop())
self._event_pipe.send(b"")
else:
Expand Down Expand Up @@ -482,13 +482,12 @@ def _schedule_flush(self):
self._flush_pending = True

# add_timeout has to be handed to the io thread via event pipe
#def _schedule_in_thread():
# def _schedule_in_thread():
# self._io_loop.call_later(self.flush_interval, self._flush)

#self.pub_thread.schedule(_schedule_in_thread)
# self.pub_thread.schedule(_schedule_in_thread)
self.pub_thread.schedule(self._flush)


def flush(self):
"""trigger actual zmq send
Expand Down
6 changes: 1 addition & 5 deletions ipykernel/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import sys
import threading
import typing as t
from contextlib import contextmanager
from functools import partial

import janus
import comm
import zmq.asyncio
from IPython.core import release
Expand Down Expand Up @@ -232,7 +229,7 @@ def start(self):
self.log.warning("debugpy_socket undefined, debugging will not be enabled")
super().start()
if self.debugpy_socket:
#asyncio.run_coroutine_threadsafe(self.process_debugpy(), self.control_thread.loop)
# asyncio.run_coroutine_threadsafe(self.process_debugpy(), self.control_thread.loop)
self.control_thread.create_task(self.process_debugpy())

def set_parent(self, ident, parent, channel="shell"):
Expand Down Expand Up @@ -364,7 +361,6 @@ async def run_cell(*args, **kwargs):
kwargs.update(
transformed_cell=transformed_cell,
preprocessing_exc_tuple=preprocessing_exc_tuple,

)
coro = run_cell(code, **kwargs)

Expand Down
3 changes: 1 addition & 2 deletions ipykernel/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,7 @@ def start(self):
return

def stop(self):
"""Stop the kernel, thread-safe.
"""
"""Stop the kernel, thread-safe."""
try:
self._stop_queue.sync_q.put(None)
except AttributeError:
Expand Down
27 changes: 7 additions & 20 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@

from ipykernel.jsonutil import json_clean

from .shell import ShellThread
from ._version import kernel_protocol_version
from .shell import ShellThread


def DEBUG(msg):
Expand Down Expand Up @@ -622,12 +622,7 @@ async def execute_request(self, socket, ident, parent):
kwargs.update(cell_id=cell_id)

reply_content = self.do_execute(
code,
silent,
store_history,
user_expressions,
allow_stdin,
**kwargs
code, silent, store_history, user_expressions, allow_stdin, **kwargs
)
DEBUG(f"execute_request {parent['header'].get('shell_id')}")

Expand Down Expand Up @@ -673,7 +668,7 @@ def do_execute(
allow_stdin=False,
*,
cell_id=None,
shell_id=None
shell_id=None,
):
"""Execute user code. Must be overridden by subclasses."""
raise NotImplementedError
Expand Down Expand Up @@ -873,9 +868,7 @@ async def is_complete_request(self, socket, ident, parent):
if inspect.isawaitable(reply_content):
reply_content = await reply_content
reply_content = json_clean(reply_content)
reply_msg = self.session.send(
socket, "is_complete_reply", reply_content, parent, ident
)
reply_msg = self.session.send(socket, "is_complete_reply", reply_content, parent, ident)
self.log.debug("%s", reply_msg)

def do_is_complete(self, code):
Expand All @@ -889,9 +882,7 @@ async def debug_request(self, socket, ident, parent):
if inspect.isawaitable(reply_content):
reply_content = await reply_content
reply_content = json_clean(reply_content)
reply_msg = self.session.send(
socket, "debug_reply", reply_content, parent, ident
)
reply_msg = self.session.send(socket, "debug_reply", reply_content, parent, ident)
self.log.debug("%s", reply_msg)

def get_process_metric_value(self, process, name, attribute=None):
Expand Down Expand Up @@ -937,9 +928,7 @@ async def usage_request(self, socket, ident, parent):
reply_content["host_cpu_percent"] = cpu_percent
reply_content["cpu_count"] = psutil.cpu_count(logical=True)
reply_content["host_virtual_memory"] = dict(psutil.virtual_memory()._asdict())
reply_msg = self.session.send(
socket, "usage_reply", reply_content, parent, ident
)
reply_msg = self.session.send(socket, "usage_reply", reply_content, parent, ident)
self.log.debug("%s", reply_msg)

async def do_debug_request(self, msg):
Expand Down Expand Up @@ -1013,9 +1002,7 @@ async def clear_request(self, socket, idents, parent): # pragma: no cover
"clear_request is deprecated in kernel_base. It is only part of IPython parallel"
)
content = self.do_clear()
self.session.send(
socket, "clear_reply", ident=idents, parent=parent, content=content
)
self.session.send(socket, "clear_reply", ident=idents, parent=parent, content=content)

def do_clear(self):
"""DEPRECATED since 4.0.3"""
Expand Down
1 change: 0 additions & 1 deletion ipykernel/shell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import Optional

from .athread import AThread

Expand Down
2 changes: 1 addition & 1 deletion ipykernel/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _prep_msg(self, *args, **kwargs):
raw_msg = self.session.msg(*args, **kwargs)
msg = self.session.serialize(raw_msg)
return msg
#return [zmq.Message(m) for m in msg]
# return [zmq.Message(m) for m in msg]

async def _wait_for_msg(self):
while not self._reply:
Expand Down
6 changes: 3 additions & 3 deletions ipykernel/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def test_io_thread():
pub = ctx.socket(zmq.PUB)
thread = IOPubThread(pub)
thread._setup_pipe_in()
#msg = [thread._pipe_uuid, b"a"]
#thread._handle_pipe_msg(msg)
# msg = [thread._pipe_uuid, b"a"]
# thread._handle_pipe_msg(msg)
ctx1, pipe = thread._setup_pipe_out()
pipe.close()
thread._pipe_in1.close()
Expand Down Expand Up @@ -118,4 +118,4 @@ def test_outstream():
assert stream.writable()
thread.stop()
thread.close()
#ctx.term()
# ctx.term()

0 comments on commit 960ff5a

Please sign in to comment.