Real time stdout redirect from a python function call to an async method
So I have a heavy time-consuming function call my_heavy_function
and I need to redirect that output to a web interface that is calling it, I have a method to send messages to the web interface, let's called that method async push_message_to_user()
.
basically, it's something like
import time
def my_heavy_function():
time_on = 0
for i in range(20):
time.sleep(1)
print(f'time spend {time_on}')
time_on = time_on+1
async def push_message_to_user(message:str):
# some lib implementation
pass
if __name__ == "__main__":
my_heavy_function() # how push prints to the UI ?
maybe there is a way giving my_heavy_function(stdout_obj)
and use that "std_object"(StringIO) to do something like stdout_object.push(f'time spend {time_on}')
. I can do that, but what I can't change the my_heavy_function()
by an async version, to add push_message_to_user()
directly instead of the print
(it's used by other non-ascyn routines)
what I would want it's something like (pseudocode)
with contextlib.redirect_output(my_prints):
my_heavy_function()
while my_prints.readable():
# realtime push
await push_message_to_user(my_prints)
Thanks!
from Recent Questions - Stack Overflow https://ift.tt/3xyTzZB
https://ift.tt/eA8V8J
Comments
Post a Comment