Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7171229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:22:20+00:00 2026-05-28T15:22:20+00:00

I have the following function, which returns a filesize of a file over HTTP:

  • 0

I have the following function, which returns a filesize of a file over HTTP:

def GetFileSize(url):
    " Function gets a url and returns it's filesize in bytes "
    url = url.replace(' ', '%20')
    u = urllib2.urlopen(url)
    meta = u.info()
    file_size = int(meta.getheaders("Content-Length")[0])
    return file_size

I would like to get the biggest file from a given links, and I wrote the following function for it:

def GetBiggestFile(links):
    " Function gets a list of links and returns the biggest file and his size in bytes "
    dic = {}
    for link in links:
        filename = link.split('/')[-1]
        filesize = GetFileSize(link)
        dic[link] = filesize
        print "%s | %.2f MB" % (filename, filesize / 1024.0 / 1024.0)

    biggest_file = max(dic, key=dic.get)
    return biggest_file, dic[biggest_file]

My lists have dozens of links, therefore this scripts takes some time to complete. Using threading I can fetch the different filesizes synchronously and shorten the running time of the code.

I’m not so sure how to do it – I’ve tried using a decorator that makes the function run asynchronously:

def run_async(func):
    " Decorator for running functions asynchronously. "
    from threading import Thread
    from functools import wraps

    @wraps(func)
    def async_func(*args, **kwargs):
        func_hl = Thread(target = func, args = args, kwargs = kwargs)
        func_hl.start()
        return func_hl

    return async_func

But I’m not sure how to make my code wait for all the answers before trying to determine who is the biggest file.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:22:22+00:00Added an answer on May 28, 2026 at 3:22 pm

    You’ll be happier with multiprocessing.

    Start with this example: http://docs.python.org/library/multiprocessing.html#using-a-pool-of-workers

    Your GetFileSize function can be run in a process pool.

    Since each process is separate, you should also have an “output Queue” into which the results are put. A separate process does a simple “get” to retrieve all the answers from the Queue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JQ-Grid with a formatter function which returns some HTML as
I have been using following function to display the file size in bytes in
I have following function which returns Table . create Function FN(@Str varchar(30)) returns @Names
I have the following JavaScript function which receives coordinates and returns the nearest tube
I have the following find function: $this->MyModel->find('all', array('conditions' => array('id' => $id))); which returns
I have the following function which works very well within a $(document).ready(function(){ $('.threadWrapper >
So: I have the following function, adapted from a formula found online, which takes
I have the following C++ function definition, which I am trying to call through
I have a for-loop which performs the following function: Take a M by 8
I have the following JavaScript code: Link In which the function makewindows does not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.