【问题标题】:Background thread clock in Jupyter labJupyter 实验室的后台线程时钟
【发布时间】:2019-08-07 08:12:48
【问题描述】:

在我的 Jupyter 笔记本中,我想执行一些长时间运行的后台作业并在单元格中显示它们的状态。我想,在做一些困难的事情之前,我可以从一个时钟开始。

我在一个单元格中有以下代码

import ipywidgets as widgets
import time
from datetime import datetime as dt
from IPython.display import HTML
from IPython.display import display
import threading

out = widgets.Output()

@out.capture(clear_output=True, wait=True)
def display_time():
    now = dt.now().strftime("%H:%M:%S")
    display(HTML(f"""<h1><center>{now}</center></h1>"""))

def run_time():
    while True:
        time.sleep(1)
        display_time()

out

当我打电话给run_time() 时,我的时间每秒都在更新,但我的笔记本在这个单元格上阻塞了。所以我尝试了threading.Thread(target=run_time).start(),但这只是在我与笔记本交互时更新时间。即使不进行任何单元格交互,如何更新笔记本?

【问题讨论】:

  • 你做到了吗?如果是这样,你用了什么?

标签: python jupyter-notebook jupyter-lab


【解决方案1】:

你想看看ipyrallel 在你的笔记本中运行多内核。

这是solved issue

这里是documentation

【讨论】:

  • 如果我理解正确,ipyrallel 通常可用于多线程 - 也可用于集群。我要做的就是启动一个单独的线程,在笔记本中显示数据。我希望有更简单的东西。
  • 是的,因此它允许您选择笔记本电脑中的哪个内核将用于运行单元。因此,您可以为 TimeClock 运行 1 个具有 1 个核心的单元,并在另一个单元上运行笔记本的其余部分。这有点棘手,但它应该可以工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-15
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
相关资源
最近更新 更多