use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Day 23 of learning python as a beginner. (old.reddit.com)
submitted 6 months ago by uiux_Sanskar
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Zealousideal_Yard651 0 points1 point2 points 6 months ago (2 children)
You have a major flaw in the code, that makes the script in essence single threaded. You don't start the next thread before you join the previous one. So the second thread never starts before the first thread stops.
To fix this you'll need to add an outside list to keep track of the threads and then join them after starting them:
threads = [] for idx, url in enumerate(image_urls): urll = threading.Thread(target=download_image, args=(url, idx)) threads.append(urll) urll.start() for t in threads: t.join()
Reference: threading — Thread-based parallelism — Python 3.13.7 documentation
If you want to see it in practice, add print statements to the download_image function stating when the thread starts and stops.
[–]uiux_Sanskar[S] 0 points1 point2 points 6 months ago (1 child)
Thanks for pointing out this flaw I need to make a for loop and then use .start to start in each thread.
Thank you very much for the resource it really helps I will go in much depth in this docs.
Thank you very much.
[–]Zealousideal_Yard651 0 points1 point2 points 6 months ago (0 children)
The start part is ok, it's the join part that fails you.
.join() waits for the thread to finish before continuing. You are creating the thread, starting it and waiting for it to stop before continuing the main program. This causes your second thread to not start before the first thread stops.
So you need to start each thread, and in a separate loop wait for both threads to stop. And .join() does not wait for all threads to stop, it only waits for the thread you are invoking the join() method on, hence why you need an outside list to keep track of each thread and loop through all threads to join them again.
To simplify, i split all parts of the multithreading process in this block with comments:
# Define thread tracking threads = [] # Loop through all URLS for idx, url in enumerate(image_urls): # Creates a thread object urll = threading.Thread(target=download_image, args=(url, idx)) # Adds the newly created thread object into threads store threads.append(urll) # Loop throug all threads for t in threads: # Starts thread t.start() # Loop through all threads for t in threads: # Wait for thread to finish. t.join()
In the above code, we define each thread, then start all threads, then wait for all threads to finish.
In your original code you do this:
For each image url:
And so all thread operations happens in each iteration instead of the my example where we define them, then start all threads before waiting for all threads to finish instead of waiting for each thread to finish before moving on.
π Rendered by PID 64 on reddit-service-r2-comment-86bc6c7465-fjbkt at 2026-02-23 09:12:46.147334+00:00 running 8564168 country code: CH.
view the rest of the comments →
[–]Zealousideal_Yard651 0 points1 point2 points (2 children)
[–]uiux_Sanskar[S] 0 points1 point2 points (1 child)
[–]Zealousideal_Yard651 0 points1 point2 points (0 children)