Issue
When I run a python module with asyncio, it works as intended when executed from cmd, but not when executed from git bash.
The code is intended to print "hello", wait 3 seconds, then print "world". It does this correctly in cmd, but in git bash, it waits three seconds first, and then prints both statements back to back.
Here is the code:
import asyncio
async def main():
print('hello')
await asyncio.sleep(3)
print('world')
asyncio.run(main())
Is there something inherent in how git bash treats I/O, that causes this behaviour?
I'm using Python 3.10.2 on Windows 10. Git version 2.24.1.windows.2.
Solution
Per Mofi's comment, I changed my code to this:
import asyncio
async def main():
print('hello', flush=True)
await asyncio.sleep(3)
print('world', flush=True)
asyncio.run(main())
This forces the buffer to flush after both prints, sending it to the terminal. It works in Git Bash now.
Details: From what I now understand, under the hood, python sets the buffering depending on the detected device. From https://docs.python.org/2/library/functions.html#open, files are "usually line buffered for tty devices and fully buffered for other files". Print uses the default sys.stdout file object to write to.
Running a new module with the following code: import sys
print(f'{sys.stdout.isatty()=}')
It prints 'sys.stdout.isatty()=True' in cmd but 'sys.stdout.isatty()=False' in git bash, which explains why stdout chooses to fully buffer in git bash, not print until the code exits, and line buffers in cmd.
Answered By - Nikita Traynin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.