r/madeinpython • • Aug 30 '20

Second part for my Threading module! Stay till the end and watch the benefits of multi-threading where I attempt downloading hundreds of images from the internet with multi threads and a single thread

https://www.youtube.com/watch?v=2MGAVyLNx44&feature=share
19 Upvotes

9 comments sorted by

4

u/CotoCoutan Aug 30 '20

Thank you for another great tutorial!

I had a question: does a thread automatically get killed & free up system resources after its job is over? Here's an example code:

import threading, time
from selenium import webdriver

def browseURLandQuit():
    driver = webdriver.Chrome()
    driver.get("https://google.com")
    time.sleep(5)
    driver.quit()

threading.Thread(target=browseURLandQuit).start()
while True:
    print("testing")
    time.sleep(5)

So does the thread that has been spawned for browseURLandQuit() automatically die & free up system resources in the above example? Or is some remnant left behind which won't get cleared until the Python file stops running? Thanks!

I'm mainly worried about threads eating up the RAM on my system (when running a script 24/7) & would like to find out some way to monitor each thread individually.

2

u/jim1930 Aug 30 '20

The answer is Yes.
You could always test it with running threading.active_count()

If you run this within your function, you will receive 2 (Main Thread + Your custom Thread)
If you run this, for instance, a line before the While true, you'll receive 1.

2

u/CotoCoutan Aug 30 '20

Thanks a ton! This is exactly what I was looking for, appreciate your help! :)

3

u/Heniadyoin1 Aug 30 '20

The GC should clean up every thing after leaving the function

1

u/CotoCoutan Aug 30 '20 edited Aug 30 '20

Thanks, I sure hope so! 🤞🏽

2

u/HandsOfSugar Aug 30 '20

Saved for later!

1

u/jim1930 Aug 30 '20

Good to know that :)

2

u/thc5 Aug 30 '20

Thanks for the post, just wanted to learn how to multithread!