adding chapter 7 source code

This commit is contained in:
muassif
2021-03-31 09:22:38 +04:00
committed by GitHub
parent 9589675a50
commit f88527eef4
20 changed files with 618 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
# thread1-extra.py to create simple threads with function (using threads list to simplly the code)
import threading
from threading import Thread as Thread
from time import sleep
def print_hello():
sleep(2)
print("{}: Hello".format(threading.current_thread().name))
def print_message(msg):
sleep(1)
print("{}: {}".format(threading.current_thread().name, msg))
threads = []
# creating threads
threads.append(Thread(target=print_hello, name="Th 1"))
threads.append(Thread(target=print_hello, name="Th 2"))
threads.append(Thread(target=print_message, args=["Good morning"], name="Th 3"))
# start the threads
for th in threads:
th.start()
# wait till all are done
for th in threads:
th.join()
+30
View File
@@ -0,0 +1,30 @@
# thread1.py to create simple threads with function
from threading import current_thread, Thread as Thread
from time import sleep
def print_hello():
sleep(2)
print("{}: Hello".format(current_thread().name))
def print_message(msg):
sleep(1)
print("{}: {}".format(current_thread().name, msg))
# creating threads
t1 = Thread(target=print_hello, name="Th 1")
t2 = Thread(target=print_hello, name="Th 2")
t3 = Thread(target=print_message, args=["Good morning"], name="Th 3")
# start the threads
t1.start()
t2.start()
t3.start()
# wait till all are done
t1.join()
t2.join()
t3.join()
+29
View File
@@ -0,0 +1,29 @@
# thread2.py to create daemon and non-daemon threads
from threading import current_thread, Thread as Thread
from time import sleep
def daeom_func():
#print(threading.current_thread().isDaemon())
sleep(3)
print("{}: Hello from daemon".format
(current_thread().name))
def nondaeom_func():
#print(threading.current_thread().isDaemon())
sleep(1)
print("{}: Hello from non-daemon".format(
current_thread().name))
# creating threads
t1 = Thread(target=daeom_func, name="Daemon Thread",daemon=True)
t2 = Thread(target=nondaeom_func, name="Non-Daemon Thread")
# start the threads
t1.start()
t2.start()
print("Exiting the main program")
+27
View File
@@ -0,0 +1,27 @@
# thread3a.py when no thread synchronization used
from threading import Thread as Thread
def inc():
global x
for _ in range(1000000):
x+=1
#global variable
x = 0
# creating threads
t1 = Thread(target=inc, name="Th 1")
t2 = Thread(target=inc, name="Th 2")
# start the threads
t1.start()
t2.start()
#wait for the threads
t1.join()
t2.join()
print("final value of x :", x)
+27
View File
@@ -0,0 +1,27 @@
# thread3b.py when thread synchronization is used
from threading import Lock, Thread as Thread
def inc_with_lock (lock):
global x
for _ in range(1000000):
lock.acquire()
x+=1
lock.release()
x = 0
mylock = Lock()
# creating threads
t1 = Thread(target=inc_with_lock , args=(mylock,), name="Th 1")
t2 = Thread(target=inc_with_lock , args=(mylock,), name="Th 2")
# start the threads
t1.start()
t2.start()
#wait for the threads
t1.join()
t2.join()
print("final value of x :", x)
+34
View File
@@ -0,0 +1,34 @@
# thread4.py with queue and custom Thread class
from queue import Queue
from threading import Thread as Thread
from time import sleep
class MyWorker (Thread):
def __init__(self, name, q):
Thread.__init__(self)
self.name = name
self.queue = q
def run(self):
while True:
item = self.queue.get()
sleep(1)
try:
print ("{}: {}".format(self.name, item))
finally:
self.queue.task_done()
#filling the queue
myqueue = Queue()
for i in range (10):
myqueue.put("Task {}".format(i+1))
# creating threads
for i in range (5):
worker = MyWorker("Th {}".format(i+1), myqueue)
worker.daemon = True
worker.start()
myqueue.join()
@@ -0,0 +1,22 @@
# this is a code without using any threads
from time import time
from getfilelistpy import getfilelist
import gdown
resource = {
"api_key": "AIzaSyDYKmm85keqnk4bF1DpYa2bxddKrGns4z0",
"id": "0B8TxHW2Ci6dbckVweTRtTlV3RUU",
"fields": "files(name,id,webContentLink)",
}
res = getfilelist.GetFileList(resource)
files_list = res['fileList'][0]
t1 = time()
for item in files_list['files']:
gdown.download( item['webContentLink'],
'./files/{}'.format(item['name']),
quiet=False)
print('Time taken to download: %s seconds', time() - t1)
@@ -0,0 +1,60 @@
#threads_casestudy.py
from queue import Queue
from threading import Thread
import time
from getfilelistpy import getfilelist
import gdown
THREAD_POOL_SIZE = 5
#update the resource object as per your API key and the gdriver folder id
resource = {
"api_key": "AIzaSyDYKmm85keq4bF1DpYa2bxddKrGns4z0",
"id": "0B8TxHW2Ci6dbckVwtTlV3RUU",
"fields": "files(name,id,webContentLink)",
}
class DownlaodWorker(Thread):
def __init__(self, name, queue):
Thread.__init__(self)
self.name = name
self.queue = queue
def run(self):
while True:
# Get the file id and name from the queue
item1 = self.queue.get()
try:
gdown.download( item1['webContentLink'],
'./files/{}'.format(item1['name']),
quiet=False)
finally:
self.queue.task_done()
def main():
def get_files(resource):
#global files_list
res = getfilelist.GetFileList(resource)
files_list = res['fileList'][0]
return files_list
start_time = time.monotonic()
files = get_files(resource)
#add files info into the queue
queue = Queue()
for item in files['files']:
queue.put(item)
for i in range (THREAD_POOL_SIZE):
worker = DownlaodWorker("Thread {}".format(i+1),queue)
worker.daemon = True
worker.start()
queue.join()
end_time = time.monotonic()
print('Time taken to download: {} seconds'.
format( end_time - start_time))
main()