sys.stdout.flush()无法与python和electronjs一起正常工作

2024-06-11 10:55:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个electronjs python应用程序,并且正在使用pythonshell模块。electron应用程序应该将我的python脚本打印的任何消息记录到控制台中,但它不会在应该打印的时候打印每条消息,而是等待脚本完成执行,然后打印所有消息。我尝试过使用sys.stdout.write("message")然后使用sys.stdout.flush(),但它仍然不起作用

我正在链接的问题与我的问题类似,但对他们有用的答案在电子应用程序中对我不起作用。它在python后端正常刷新,前端是导致问题的原因

类似问题:Python sys.stdout.flush() doesn't work


Tags: 模块答案脚本应用程序消息message链接stdout
1条回答
网友
1楼 · 发布于 2024-06-11 10:55:48

file.flush()不一定要将文件的数据写入磁盘!。您需要使用flush()后跟os.fsync(fd)来确保这种行为

见下文:

sys.stdout.flush()
os.fsync(sys.stdout.fileno())

os.fsync(fd)python文档中的文档

Force write of file with file descriptor fd to disk. On Unix, this calls the native fsync() function; on Windows, the MS _commit() function.

If you’re starting with a Python file object f, first do f.flush(), and then do os.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.

相关问题 更多 >