Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!