Skip to content

Commit 95a65d5

Browse files
echarlesCarreau
authored andcommitted
guard on first cpu request returning None or 0.0
1 parent 5602f43 commit 95a65d5

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

ipykernel/kernelbase.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,11 @@ async def usage_request(self, stream, ident, parent):
867867
if inspect.isawaitable(reply_content):
868868
reply_content = await reply_content
869869
reply_content = json_clean(reply_content)
870-
reply_content['cpu_percent'] = psutil.cpu_percent()
870+
cpu_percent = psutil.cpu_percent()
871+
# https://psutil.readthedocs.io/en/latest/index.html?highlight=cpu#psutil.cpu_percent
872+
# The first time cpu_percent is called it will return a meaningless 0.0 value which you are supposed to ignore.
873+
if cpu_percent != None and cpu_percent != 0.0:
874+
reply_content['cpu_percent'] = cpu_percent
871875
reply_content['virtual_memory'] = psutil.virtual_memory()
872876
reply_content['virtual_memory_dict'] = dict(psutil.virtual_memory()._asdict())
873877
reply_content['virtual_memory_percent'] = psutil.virtual_memory().percent

0 commit comments

Comments
 (0)