在长时间执行单元格时显示消息

时间:2019-04-24 18:49:11

标签: python jupyter-notebook

我正在研究一个Jupyter笔记本,其中一个单元需要一段时间才能生成一系列图形。为了向用户保证平台不会发生故障并且单元未冻结,我想每5秒执行一次print('computing')之类的操作,直到单元执行完毕。

在jupyter笔记本环境中是否有直接的方法来执行此操作?我已经探索了一些本机计时功能,但似乎没有什么可以做到的。

1 个答案:

答案 0 :(得分:2)

您可以使用threadingmultiprocessing进行此操作,我在下面使用了线程。

import time
from threading import Thread

def progress(stop):
    while True:
        print('Cell Running...')
        time.sleep(5)
        if stop():
            break

stop_threads = False
t1 = Thread(target=progress, args=(lambda: stop_threads, ))
t1.start()

# do main here
print('from main')
time.sleep(6)
print('from main 2')
stop_threads = True

# join your thread
t1.join()
相关问题