Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush after writting JSON to stdout #727

Closed
ycrack84 opened this issue May 1, 2020 · 3 comments
Closed

Flush after writting JSON to stdout #727

ycrack84 opened this issue May 1, 2020 · 3 comments

Comments

@ycrack84
Copy link

ycrack84 commented May 1, 2020

In some cases, in resulting JSON is missing last few lines or output is missing entirely.
This could be fixed by flushing buffer to file after write

fp.write("\n")

fp.flush()

@mikf
Copy link
Owner

mikf commented May 10, 2020

Writing a newline to stdout (as well as exiting the process) already causes a flush() to happen,
as can be seen with the following example code:

import sys
import time

for i in range(3):
    sys.stdout.write("foo")
    time.sleep(1)

sys.stdout.write("\n")  # causes "foofoofoo" to appear
sys.stdout.write("bar")  # doesn't get shown/flushed until process ends
time.sleep(3)

What exactly are "some cases"? What are you trying to do where it doesn't work?

@ycrack84
Copy link
Author

Hi @mikf sorry for late response.
Executing command gallery-dl -j "https://www.imagefap.com/profile/Nika" prints nothing to console, but it depends on the size of resulting json and buffer size.

I found out that problem is only with git bash (mingw64). Windows cmd and powershell prints whole json (without explicit flush).
The only way to fix it for git bash is to flush buffer after write.

gallery-dl v1.14.1
OS: Win 10
Python: 3.8.2

mikf added a commit that referenced this issue Jun 21, 2020
… and remove the dead handle_finalize() method,
which is never called since DataJob() overrides run().
@mikf
Copy link
Owner

mikf commented Jun 25, 2020

There are apparently some problems with cygwin/mingw and Python's detection whether stdout etc is a TTY or not and its auto-flush behavior, which I've never heard about before:

Well, I've now added an explicit flush() after calling dump_json() when displaying the results for -j/--dump-json (1b3870a). I hope that helps.

@mikf mikf closed this as completed Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants