我想在另一个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只是一个例子。我想使用其他方法,但是结果是一样的。
添加回答
举报
0/150
提交
取消