Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda