apparent_encoding should be cached since chardet can be slow
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 48/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Estancado
- Stack tecnológico
- python
- Área
- api, performance
Línea de trabajo
Comienza por la propiedad Response.apparent_encoding y su uso desde la propiedad r.text. Reproduce accesos repetidos al texto con contenido que requiera detección y, después, revisa las pruebas existentes de response para encontrar una prueba de regresión adecuada. Se considera terminado cuando los accesos repetidos calculen la apparent encoding solo una vez por response.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
We have some scraper code that sometimes gets back PDFs and other times gets back HTML. Today we learned that if you access r.text in a large-ish PDF (40MB), chardet is called, which uses a lot of CPU (and a ton of memory):
r = requests.get(some_url)
r.text
That's more or less fine (best not to try to get the text of a PDF this way), but if you access r.text more than once, chardet gets run over and over.
We have code like this that performs horribly:
r = requests.get(some_url)
if bad_text in r.text:
continue
if other_bad_text in r.text:
continue
# ...many more tests...
When you access r.text, it checks if the encoding can come from the HTTP headers. If not, it runs the apparent_encoding property, which looks like:
@property
def apparent_encoding(self):
"""The apparent encoding, provided by the charset_normalizer or chardet libraries."""
return chardet.detect(self.content)["encoding"]
I think that property should probably be cached since it's slow, so that repeated calls to r.text don't hurt so badly.
Expected Result
I expected the calls to the text property to only calculate the encoding once per request.
Actual Result
Each call to the text property re-calculates the encoding, which is slow and uses a lot of memory (this is probably a bug in chardet, but it uses hundreds of MB on a 40MB PDF right now).
System Information
$ python -m requests.help
{
"chardet": {
"version": "5.0.0"
},
"charset_normalizer": {
"version": "2.0.12"
},
"cryptography": {
"version": "36.0.2"
},
"idna": {
"version": "2.10"
},
"implementation": {
"name": "CPython",
"version": "3.10.7"
},
"platform": {
"release": "5.4.209-116.363.amzn2.x86_64",
"system": "Linux"
},
"pyOpenSSL": {
"openssl_version": "101010ef",
"version": "20.0.1"
},
"requests": {
"version": "2.28.1"
},
"system_ssl": {
"version": "101010ef"
},
"urllib3": {
"version": "1.26.5"
},
"using_charset_normalizer": false,
"using_pyopenssl": true
}
- Lenguaje dominante
- Python
- Estrellas
- 54.7k
- Forks
- 10.8k
- Merge medio
- 18 min
- PR fusionados (30 d)
- 1
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de psf/requests
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
-
`iter_lines(decode_unicode=True, delimiter=<str>)` raises `TypeError` when `Response.encoding` is `None`Quizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
-
get_encoding_from_headers treats any content type containing 'text' as text/* (ISO-8859-1)Posiblemente ocupada @suyashgitt la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
Invalid `params` type (e.g. int) raises misleading "TypeError: Cannot mix str and non-str arguments"Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Todos los issues de psf/requests
Issues similares
-
README Quick Start query command fails, and doctor points to a missing Prerequisites sectionAbiertodocumentation type/bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
add entry to blockAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 65/100
StevenBlack/hosts#3299 ·
-
`garak --report` overwrites the input report when its name has no ".report"Posiblemente ocupada @0z5a la tomó hoy. Abiertoneeds-triage
Dificultad 2/5 Medio día Aptitud para principiantes 72/100
Los mantenedores suelen responder en 2 días
-
trace: Pi's 1-hour cache writes are priced at 1.25x, but billed at 2xPosiblemente ocupada @Shizoqua la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
microsoft/TokenEconomizer#221 ·
Los mantenedores suelen responder en 1 día
-
plot_graph crashes when a mesh node has no m2m edgePosiblemente ocupada @tygvi la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
mllam/neural-lam#883 · 1 comentario ·
Los mantenedores suelen responder en 2 días