Make print() use a single write() call

Abierto
#161,934 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
22/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
python
Área
backend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
Python
Estrellas
78.2k
Forks
36.6k
Merge medio
19 h 57 min
PR fusionados (30 d)
630

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python/cpython

Todos los issues de python/cpython

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.