使用JavaScrip执行带延时的Python脚本

2024-09-30 22:18:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过JavaScript文件执行Python脚本,因为我想将Python脚本的结果打印到html页面。我首先研究了python-shell模块。我遵循了以下教程:

http://ourcodeworld.com/articles/read/286/how-to-execute-a-python-script-and-retrieve-output-data-and-errors-in-node-js

我成功地遵循了教程,一切正常。你知道吗

不过,我现在想在控制台中延迟打印一些数字。我的Python脚本包含以下代码:

while True:
    print(random.randint(1,101))
    time.sleep(10)

当我运行JavaScript代码时,什么也没发生。我做了一些调试,删除了time.sleep()语句,它就工作了。你知道吗

正确的方法是什么?当我删除time.sleep()时,为什么这会起作用?你知道吗


Tags: 模块and文件代码脚本comhttptime
1条回答
网友
1楼 · 发布于 2024-09-30 22:18:34

打印之后,需要刷新python脚本中的标准输出流。你知道吗

import sys

while True:
    print(random.randint(1,101))
    sys.stdout.flush()
    time.sleep(10)

相关问题 更多 >