I am running code as follows (copied from other topic and added sleep
):
import sys
import StringIO
import contextlib
@contextlib.contextmanager
def stdoutIO(stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO.StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
code = """
import time
i = [0,1,2]
for j in i :
print j
time.sleep(5)
"""
with stdoutIO() as s:
exec code
print "out:", s.getvalue()
When I run this code I have to wait 15 seconds until the program ends to see the output.
But what should I do to see the output of the print
statement in each iteration of the for
loop every 5 seconds? Not to wait 15 seconds to see the whole output.
Is this possible at all? Is it possible to see the current output of exec?