Skip to content

Instantly share code, notes, and snippets.

@gordinmitya
Last active September 17, 2019 10:12
Show Gist options
  • Save gordinmitya/a517924f9bb12324c37c377e5184d0ee to your computer and use it in GitHub Desktop.
Save gordinmitya/a517924f9bb12324c37c377e5184d0ee to your computer and use it in GitHub Desktop.
distributed systems: lab 2
class DownloadThread(Thread):
def __init__(self, url):
super().__init__()
self.url = url
def run(self):
handle = urllib.request.urlopen(self.url)
fname = os.path.basename(self.url)
with open(fname, "wb") as f_handler:
while True:
chunk = handle.read(1024)
if not chunk:
break
f_handler.write(chunk)
#!/usr/bin/python3
import threading
from threading import Thread
import time
def func(a, b, name):
while True:
a = a + b
print("{}: {}".format(name, a))
time.sleep(1)
if __name__ == "__main__":
thread1 = Thread(target=func, args=(5, 6, "thread1"))
thread2 = Thread(target=func, args=(1, 2, "thread2"))
thread1.start()
thread2.start()
while True:
print("Total number of threads: {}".format(threading.activeCount()))
print("List of threads: {}".format(threading.enumerate()))
time.sleep(5)
#!/usr/bin/python3
from threading import Thread
import time
list = []
def func(a):
time.sleep(1)
list.append(a)
if __name__ == "__main__":
thread1 = Thread(target=func, args=(1,))
thread1.start()
thread2 = Thread(target=func, args=(6,))
thread2.start()
print("not empty list: ", list)
#!/usr/bin/python3
from threading import Thread
import time
class MyThread(Thread):
def __init__(self, threadID, name, delay):
super().__init__()
self.threadID = threadID
self.name = name
self.delay = delay
def run(self):
print("Starting ", self.name)
print_time(self.name, 5, self.delay)
print("Exiting ", self.name)
def print_time(threadName, counter, delay):
while counter:
time.sleep(delay)
print("%s: %s sec" % (threadName, time.strftime("%S", time.gmtime())))
counter -= 1
if __name__ == "__main__":
# Create new threads
thread1 = MyThread(1, "Thread-1", 1)
thread2 = MyThread(2, "Thread-2", 2)
# Start new Threads
thread1.start()
thread2.start()
print("Exiting Main Thread")
#!/usr/bin/python3
import hashlib
from threading import Thread
from multiprocessing import Process
from timeit import timeit
# hashes of random str(int) from [0 ... 1_000_000]
TASKS = ['604678604882550e79d90fd9b29ecf34', '87456e18f180720ebaaf070f7d1e6e1c', '98222663d6fe9ea55efff46179d7c9b2',
'c64a9829fa4638ff5de86330dd227e35', 'ca6bff62f4e46cbb192152ec843ebdbf', 'df7c2b3c3966426c14e4b3005c931eb1',
'626103abae1be890f7d1c8148f9d690a', 'e31d05da308bf27ad15fedde779f2bc5', 'a2b12d7cf762d6cfb6ea086f9f492626',
'a8573e231edaaedfb49ebfc14f4be808']
def solve(task):
for i in range(10 ** 6):
h = hashlib.md5(str(i).encode("utf-8")).hexdigest()
if h == task:
return
def multi():
executors = []
for task in TASKS:
e = Thread(target=solve, args=(task,))
e.start()
executors.append(e)
for e in executors:
e.join()
def single():
for task in TASKS:
solve(task)
if __name__ == '__main__':
# run function `number` times and return avg execution time
BENCH_COUNT = 10
res = timeit(multi, number=BENCH_COUNT)/BENCH_COUNT
print("multi_thread", res)
res = timeit(single, number=BENCH_COUNT)/BENCH_COUNT
print("single_thread", res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment