为了账号安全,请及时绑定邮箱和手机立即绑定

子流程:如果输出之间的时间过长,则无法获得实时输出

子流程:如果输出之间的时间过长,则无法获得实时输出

料青山看我应如是 2021-05-14 17:15:52
我想在另一个python脚本(run_python_script.py)中启动python脚本(我将其称为test.py)。为此,我使用如下所示的subprocess命令:https : //stackoverflow.com/a/18422264。这样就是run_python_script.py:import subprocessimport sysimport ioimport timedef runPython(filename, filename_log="log.txt"):    with io.open(filename_log, 'wb') as writer, io.open(filename_log, 'rb', 1) as reader:        process = subprocess.Popen("python {}".format(filename), stdout=writer)        while process.poll() is None:            sys.stdout.write(reader.read().decode("utf-8"))            time.sleep(0.5)        # Read the remaining        sys.stdout.write(reader.read().decode("utf-8"))runPython("test.py")这是test.py:import timesleep_time = 0.0001start_time = time.time()for i in range(10000):    print(i)    time.sleep(sleep_time)print(time.time() - start_time)在此设置中,实时输出有效,但如果sleep_time(在test.py中)很大,例如sleep_time = 1。run_python_script.py仅在test.py完成后输出。我用其他函数替换了time.sleep(sleep_time),并且每个函数花费很长时间,破坏了实时输出。corse的test.py只是一个例子。我想使用其他方法,但是结果是一样的。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 109 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号