Make print() use a single write() call

Offen
#161,934 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
22/100
Issue-Typ
Feature
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
backend

Rechercherichtung

The builtin print() lives in Python/bltinmodule.c (builtin_print_impl), which currently issues separate writes for each argument, the separator and end. Read that function first, then the existing tests in Lib/test/test_builtin.py covering print to see the expected partial-output and flush behaviour. Done means one buffered write per call, partial output preserved when str() raises, plus tests for the atomicity and error paths.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

interpreter-core topic-IO type-feature
Feature or enhancement

Currently, print("a", "b") performs four separate writes:

file.write("a")
file.write(" ")
file.write("b")
file.write("\n")

The proposed behavior is equivalent to:

file.write("a b\n")

The main motivation is to reduce interleaving between concurrent print() calls. For example, two threads printing separate messages can currently produce msg1msg2\n\n. Atomicity would still depend on the underlying stream.

Reducing the number of calls can also improve performance, particularly for streams with expensive write() methods, such as IDLE’s output stream.

The implementation should preserve partial output when converting an argument with str() raises: write the parts collected so far, without appending end, and propagate the exception. If flush=True, flush that partial output as well.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/make-print-use-a-single-write-call/109886

Linked PRs
  • gh-161936
Vorherrschende Sprache
Python
Sterne
78.2k
Forks
36.6k
Ø Merge
19 Std. 57 Min.
Gemergte PRs (30 T.)
630

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/cpython

Alle Issues in python/cpython

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.