简体   繁体   English

Python Notebook 定期自动运行

[英]Python Notebook run automatically periodically

I have a script in a notebook browser that does some live data analysis, that I would like to run periodically by itself once every few seconds/minutes, rather than I having to do Ctrl+Enter all the time.我在笔记本浏览器中有一个脚本,可以进行一些实时数据分析,我希望每隔几秒/分钟就自己定期运行一次,而不是一直按 Ctrl+Enter。

Is there any way to achieve this?有没有办法实现这一目标? Do I need any additional plug-ins, and which ones我是否需要任何额外的插件,以及哪些

Simplest way to do it:最简单的方法:

import time
def periodic_work(interval):
    while True:
        #change this to the function you want to call, or paste in the code you want to run
        your_function()
        #interval should be an integer, the number of seconds to wait
        time.sleep(interval)

To run once per minute, run a new cell with periodic_work(60) -- you should see a closed circle in the top right of the IPython Notebook that indicates the Kernal is busy.要每分钟运行一次,请使用periodic_work(60)运行一个新单元格——您应该在 IPython Notebook 的右上角看到一个封闭的圆圈,表明内核正忙。 If/when you want to stop the live update, hit the Stop button (labeled Interrupt) on the menu bar and wait a second or so.如果/当你想停止实时更新,点击菜单栏上的停止按钮(标记为中断)并等待一秒钟左右。 To start it up again, run the cell that calls periodic_work again.要再次启动它,请再次运行调用periodic_work的单元。

One way would be to make your cell a function.一种方法是让你的细胞成为一个功能。 In another cell, just call time.sleep () in an appropriate loop.在另一个单元格中,只需在适当的循环中调用 time.sleep()。 You could also have it check for the existence of some external resource to quit, if you don't want to loop an a priori known finite number of times.如果您不想循环先验已知的有限次数,您也可以让它检查是否存在一些要退出的外部资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM