Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
How to improve performance of converting data to json format?

I have the following code to convert a data(row data from postgress) to json. Usually len(data) = 100 000

def convert_to_json(self, data):
    s3 = self.session.client('s3')
    infos = {
        'videos':[],
        'total_count': len(data)
    }

    for row in data:
        video_id = row[0]
        url = s3.generate_presigned_url(
            ClientMethod='get_object',
            Params={
                'Bucket': '...',
                'Key': '{}.mp4'.format(video_id)
            }
        )

        dictionary = {
            'id': video_id,
            'location': row[1],
            'src': url
        }
        infos['videos'].append(dictionary)

    return json.dumps(infos)

Thanks for any ideas.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Most of the time in your program is probably wasted by waiting for the network. Indeed you call s3.generate_presigned_url which will send a request to Amazon and then you have to wait until the server finally responds. In the meantime there is no much processing you can do.

So the most potential is to speed the process up by doing requests in parallel. So you send for instance 10 requests and then wait for the 10 responses. This article gives a brief introduction on this.

Based on your question, and the article, you can use something like the following to speed up the process:

from multiprocessing.pool import ThreadPool

# ...

def fetch_generate_presigned_url(video_id):
    return s3.generate_presigned_url(
               ClientMethod='get_object',
               Params={
                   'Bucket': '...',
                   'Key': '{}.mp4'.format(video_id)
               }
           )

def convert_to_json(self, data):
    pool = ThreadPool(processes=10)
    urls = [row[0] for row in data]
    video_ids = pool.map(fetch_generate_presigned_url,urls)
    infos = {
        'videos':[{'id': video_id,'location': row[1],'src': row[0]}
                       for vide_id,row in zip(video_ids,data)],
        'total_count': len(data)
    }
    return json.dumps(infos)

The number of processes, process=10 can be set higher to make the requests more parallel.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda