gpt4 book ai didi

javascript - 使用 JavaScript 延迟执行 Python 脚本

转载 作者:行者123 更新时间:2023-12-03 04:30:33 25 4
gpt4 key购买 nike

我想通过 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() 后它会起作用?

最佳答案

打印后,您需要刷新 python 脚本中的标准输出流。

import sys

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

关于javascript - 使用 JavaScript 延迟执行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43509593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com