Page 1 of 1

Why does the flush keyword not work with print()?

Posted: Fri Feb 12, 2021 10:09 pm
by Simpler1
I am printing a string immediately before going into lightsleep, but the string doesn't complete until the sleep command is complete. The "flush" keyword on the print command should alleviate this, but it doesn't seem to work.

Code: Select all

print('x', flush=True)
produces

Code: Select all

File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given
yet, the definition of print shows that flush is a valid keyword:

Code: Select all

def print(
        *values: object, sep: Optional[Text] = ..., end: Optional[Text] = ..., file: Optional[_Writer] = ..., flush: bool = ...
    ) -> None: ...

Re: Why does the flush keyword not work with print()?

Posted: Mon Feb 15, 2021 3:03 am
by jimmo
Simpler1 wrote:
Fri Feb 12, 2021 10:09 pm
The "flush" keyword on the print command should alleviate this, but it doesn't seem to work.
Unfortunately MicroPython doesn't support the flush kwarg on print.

This does sound like a useful feature in this particular case though... I'm not sure if there's a workaround. Are you writing to a UART or a USB VCP?
Simpler1 wrote:
Fri Feb 12, 2021 10:09 pm
yet, the definition of print shows that flush is a valid keyword:
This definition comes from (I'm guessing) CPython.

Re: Why does the flush keyword not work with print()?

Posted: Mon Feb 15, 2021 3:23 am
by Simpler1
Hi jimmo,

I was just creating some code to test the lightsleep and deepsleep modes on the ESP32-CAM when I noticed this limitation.

I was just surprised that flush didn't work with print.