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 8942967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:39:32+00:00 2026-06-15T11:39:32+00:00

Excuse the unhelpful variable names and unnecessarily bloated code, but I just quickly whipped

  • 0

Excuse the unhelpful variable names and unnecessarily bloated code, but I just quickly whipped this together and haven’t had time to optimise or tidy up yet.

I wrote this program to dump all the images my friend and I had sent to each other using a webcam photo sharing service ( 321cheese.com ) by parsing a message log for the URLs. The problem is that my multithreading doesn’t seem to work.

At the bottom of my code, you’ll see my commented-out non-multithreaded download method, which consistently produces the correct results (which is 121 photos in this case). But when I try to send this action to a new thread, the program sometimes downloads 112 photos, sometimes 90, sometimes 115 photos, etc, but never gives out the correct result.

Why would this create a problem? Should I limit the number of simultaneous threads (and how)?

import urllib
import thread

def getName(input):
    l = input.split(".com/")
    m = l[1]
    return m

def parseMessages():
    theFile = open('messages.html', 'r')
    theLines = theFile.readlines()
    theFile.close()
    theNewFile = open('new321.txt','w')
    for z in theLines:
        if "321cheese" in z:
            theNewFile.write(z)
    theNewFile.close()

def downloadImage(inputURL):
    urllib.urlretrieve (inputURL, "./grabNew/" + d)

parseMessages()

f = open('new321.txt', 'r')
lines = f.readlines()
f.close()

g = open('output.txt', 'w')

for x in lines:
  a = x.split("<a href=\"")
  b = a[1].split("\"")
  c = b[0]
  if ".png" in c:
    d = getName(c)
    g.write(c+"\n")
    thread.start_new_thread( downloadImage, (c,) )
    ##downloadImage(c)

g.close()
  • 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-06-15T11:39:33+00:00Added an answer on June 15, 2026 at 11:39 am

    There are multiple issues in your code.

    The main issue is d global name usage in multiple threads. To fix it, pass the name explicitly as an argument to downloadImage().

    The easy way (code-wise) to limit the number of concurrent downloads is to use concurrent.futures (available on Python 2 as futures) or multiprocessing.Pool:

    #!/usr/bin/env python
    import urllib
    from multiprocessing import Pool
    from posixpath import basename
    from urllib import unquote
    from urlparse import urlsplit
    
    
    download_dir = "grabNew"
    
    def url2filename(url):
        return basename(unquote(urlsplit(url).path).decode('utf-8'))
    
    def download_image(url):
        filename = None
        try:
            filename = os.path.join(download_dir, url2filename(url))
            return urllib.urlretrieve(url, filename), None
        except Exception as e:
            return (filename, None), e
    
    def main():
        pool = Pool(processes=10)
        for (filename, headers), error in pool.imap_unordered(download_image, get_urls()):
            pass # do something with the downloaded file or handle an error
    
    if __name__ == "__main__":
       main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Excuse the beginner level of this question. I have the following simple code, but
Excuse the n00bness of this question, but I have a web application where I
Excuse if this is more a file format conversion question rather than programming, but
Excuse me if this is a silly question but i'm a beginer here. I
Excuse me if this is a sparse question but, I'm somehow confused wit the
Excuse me if this is a dupe, but I couldn't seem to get the
Excuse the beginner question, but I'm having difficulty wrapping my head around this. Using
Excuse what is probably a really basic question but how do I achieve this.
Excuse the simplicity of this request - but is there a way for me
excuse my english but nobody can answer in French ;-) I'm doing this request

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.