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

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Simpler1
Posts: 16
Joined: Wed Jan 06, 2021 6:30 pm

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

Post by Simpler1 » Fri Feb 12, 2021 10:09 pm

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: ...

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

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

Post by jimmo » Mon Feb 15, 2021 3:03 am

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.

Simpler1
Posts: 16
Joined: Wed Jan 06, 2021 6:30 pm

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

Post by Simpler1 » Mon Feb 15, 2021 3:23 am

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.

Post Reply