I'm currently using some functions to store all print
in Python
And it get all print
AFTER the function has finished
Here is a sample snippet
import io
import sys
import time
import traceback
def return_std(func):
def wrapper(*args, **kwargs):
old_stdout = sys.stdout
sys.stdout = buffer = io.StringIO()
try:
func(*args, **kwargs)
except:
print(traceback.format_exc())
finally:
sys.stdout = old_stdout
body = buffer.getvalue()
buffer.close()
return body
return wrapper
@return_std
def some_useful_function():
print('so this line is executed at ' + time.strftime('%X'))
time.sleep(5)
print('program execute at ' + time.strftime('%X'))
std_of_some_useful_function = some_useful_function()
print(std_of_some_useful_function)
print('but we can not know if it succeeded or failed before ' + time.strftime('%X'))
output
program execute at 11:11:11
so this line is executed at 11:11:11
but we can not know if it succeeded or failed before 11:11:16
This way captures all print
and error in some_useful_function
, and stores it as the function return. But print
will not show BEFORE this function has finished
How should I modify the wrapper return_std
, so I can see the output at 11:11:11
instantly (like it was really printed), and get all previous print
stored at 11:11:16