Skip to content

Unbuffered stdout #305

@dlech

Description

@dlech

Is your feature request related to a problem? Please describe.
Yes. (See pybricks/pybricksdev#16).

Since stdout is buffered, it is possible for the user program to end before all data has been sent over Bluetooth.

This means that connected devices don't know when it is safe to disconnect since even though the program has ended, there still may be more data to receive.

Describe the solution you'd like
When a user program ends, we should be sure to drain the stdout buffer before exiting the MicroPython runtime. This way, we can be sure that when the user program running status flag is set to false that there is nothing left to be received from the stdout stream.

Describe alternatives you've considered
We could also be more like CPython (see below) and drain the stdout buffer during each write. This means that, for example, a print() would always block until everything is sent over Bluetooth. The current behavior, on the other hand, will return immediately from a print() unless the buffer is full.

Additional context
In CPython, there is the -u command line option and corresponding PYTHONUNBUFFERED environment variable to deal with this problem. (Although in CPython the data is always lost instead of just delayed.)

In Python 3.7, stdout was changed so that the text part of it is always unbuffered.

Metadata

Metadata

Assignees

No one assigned

    Labels

    software: pybricks-micropythonIssues with Pybricks MicroPython firmware (or EV3 runtime)topic: MicroPythonIssues related to the MicroPython programming language

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions