Skip to content

Commit 707e557

Browse files
committed
Simplified clear task based on feedback
1 parent f3df863 commit 707e557

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

jupyter_server_documents/kernels/kernel_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def handle_incoming_message(self, channel_name: str, msg: list[bytes]):
110110
if cell_id:
111111
existing = self.message_cache.get(cell_id=cell_id)
112112
if existing and existing['msg_id'] != msg_id:
113-
self.output_processor.clear_cell_outputs(cell_id)
113+
asyncio.create_task(self.output_processor.clear_cell_outputs(cell_id))
114114

115115
self.message_cache.add({
116116
"msg_id": msg_id,

jupyter_server_documents/outputs/output_processor.py

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,16 @@ async def _clear_ydoc_outputs(self, cell_id):
6969
target_cell["outputs"].clear()
7070
self.log.info(f"Cleared outputs for {self._file_id=}, {cell_index=}")
7171

72-
def clear_cell_outputs(self, cell_id) -> asyncio.Task | None:
73-
"""
74-
Clears all outputs for a cell on disk and in ydoc. Returns an
75-
`asyncio.Task` that clears outputs for the cell in ydoc. Callers
76-
should wait for this task to complete, if they expect to update
77-
the ydoc.
78-
79-
```
80-
clear_output_task = clear("cellid")
81-
await clear_outputs_task
82-
```
83-
"""
84-
85-
clear_outputs_task = None
72+
async def clear_cell_outputs(self, cell_id):
73+
"""Clears all outputs for a cell on disk and in ydoc."""
8674

8775
if self._file_id is not None:
76+
await self._clear_ydoc_outputs(cell_id)
77+
self._pending_clear_output_cells.discard(cell_id)
78+
8879
if self.use_outputs_service:
89-
clear_outputs_task = asyncio.create_task(
90-
self._clear_ydoc_outputs(cell_id)
91-
)
9280
self.outputs_manager.clear(file_id=self._file_id, cell_id=cell_id)
9381

94-
self._pending_clear_output_cells.discard(cell_id)
95-
96-
return clear_outputs_task
9782

9883
def process_output(self, msg_type: str, cell_id: str, content: dict):
9984
"""Process outgoing messages from the kernel.
@@ -108,28 +93,28 @@ def process_output(self, msg_type: str, cell_id: str, content: dict):
10893
The content has not been deserialized yet as we need to verify we
10994
should process it.
11095
"""
111-
asyncio.create_task(self.output_task(msg_type, cell_id, content))
96+
if msg_type == "clear_output":
97+
asyncio.create_task(self.clear_output_task(cell_id, content))
98+
else:
99+
asyncio.create_task(self.output_task(msg_type, cell_id, content))
100+
112101
return None # Don't allow the original message to propagate to the frontend
113102

114-
async def output_task(self, msg_type, cell_id, content):
115-
"""A coroutine to handle output messages."""
103+
async def clear_output_task(self, cell_id, content):
104+
"""A courotine to handle clear_output messages"""
116105

117-
if msg_type == "clear_output":
118-
wait = content.get("wait", False)
119-
if not wait:
120-
clear_task = self.clear_cell_outputs(cell_id)
121-
if clear_task:
122-
await clear_task
123-
else:
124-
self._pending_clear_output_cells.add(cell_id)
106+
wait = content.get("wait", False)
107+
if wait:
108+
self._pending_clear_output_cells.add(cell_id)
109+
else:
110+
await self.clear_cell_outputs(cell_id)
125111

126-
return
112+
async def output_task(self, msg_type, cell_id, content):
113+
"""A coroutine to handle output messages."""
127114

128115
# Check for pending clear_output before processing output
129-
if cell_id in self._pending_clear_output_cells and (
130-
clear_task := self.clear_cell_outputs(cell_id)
131-
):
132-
await clear_task
116+
if cell_id in self._pending_clear_output_cells:
117+
await self.clear_cell_outputs(cell_id)
133118

134119
try:
135120
# TODO: The session manager may have multiple notebooks connected to the kernel

0 commit comments

Comments
 (0)