Skip to content

Instantly share code, notes, and snippets.

@1UC1F3R616
Last active October 29, 2021 05:43
Show Gist options
  • Save 1UC1F3R616/b8bab28d12b19eb88ff4b8dd50cc0ad6 to your computer and use it in GitHub Desktop.
Save 1UC1F3R616/b8bab28d12b19eb88ff4b8dd50cc0ad6 to your computer and use it in GitHub Desktop.
  • Most Basic
import threading
threading.Thread(target=f, args=(a,b,c)).start()
  • Example 1
import queue
import threading
import urllib2

# Called by each thread
def get_url(q, url):
    q.put(urllib2.urlopen(url).read())

theurls = ["http://google.com", "http://yahoo.com"]

q = queue.Queue()

for u in theurls:
    t = threading.Thread(target=get_url, args = (q,u))
    t.daemon = True
    t.start()

s = list(q.queue)
print(s)

# Here program exits without complete execution because treads are daemonic and hence main thread gives no fuck for them
# if we use t.join() just bellow the t.start() then everything happens in a sync but it destroys the purpose of threading as sequential execution will be happening now
# if we have t.daemon=False then program will exit after all spawned threads have finished their work
@1UC1F3R616
Copy link
Author

@1UC1F3R616
Copy link
Author

@1UC1F3R616
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment