我有一些python代码,我想打开它作为子进程。 我试过通过subprocess.Popen()以及从另一个Python脚本的asyncio线程化进程。我知道添加time.sleep()会阻止在程序生成输出时读取进程输出,但耗时的大循环具有相同的效果。有趣的是,我尝试的任何东西都是从Eclipse和PyDev一起工作的,但是如果我从shell运行我的程序,它只会在程序最终运行完毕时输出。我需要能够在程序输出到stdout时读取输出。任何帮助将不胜感激。
# would like to call this python script as a subprocess from another program.
import time
import sys
for j in range( 50000000000 ):
print("blah") # if this is removed asynchronous reading occurs just fine
time.sleep(1)
#for k in range( 10000000 ): # this would also cause output to wait until program is complete
# b=2
print("\n"*2)