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
+31
View File
@@ -0,0 +1,31 @@
# process1.py to create simple processes with function
import os
from multiprocessing import Process, current_process as cp
from time import sleep
def print_hello():
sleep(2)
print("{}-{}: Hello".format(os.getpid(), cp().name))
def print_message(msg):
sleep(1)
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
processes = []
# creating process
processes.append(Process(target=print_hello, name="Process 1"))
processes.append(Process(target=print_hello, name="Process 2"))
processes.append(Process(target=print_message,
args=["Good morning"], name="Process 3"))
# start the process
for p in processes:
p.start()
# wait till all are done
for p in processes:
p.join()
print("Exiting the main process")
+15
View File
@@ -0,0 +1,15 @@
# process2.py to create processes using a pool
import os
from multiprocessing import Process, Pool, current_process as cp
from time import sleep
def print_message(msg):
sleep(1)
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
# creating process from a pool
with Pool(3) as proc:
proc.map(print_message, ["Orange", "Apple", "Banana",
"Grapes","Pears"])
print("Exiting the main process")
+28
View File
@@ -0,0 +1,28 @@
# process3.py to use shared memory ctype objects
import multiprocessing
from multiprocessing import Process, Pool, current_process as cp
def inc_sum_list(list, inc_list, sum):
sum.value = 0
for index, num in enumerate(list):
inc_list[index] = num + 1
sum.value = sum.value + inc_list[index]
mylist = [2, 5, 7]
inc_list = multiprocessing.Array('i', 3)
sum = multiprocessing.Value('i')
p = Process(target=inc_sum_list,
args=(mylist, inc_list, sum))
p.start()
p.join()
print("incremented list: ", list(inc_list))
print("sum of inc list: ", sum.value)
print("Exiting the main process")
+29
View File
@@ -0,0 +1,29 @@
# process4.py to use shared memory using the server process
import multiprocessing
from multiprocessing import Process
def insert_data (dict1, code, subject):
dict1[code] = subject
def output(dict1):
print("Dictionary data: ", dict1)
with multiprocessing.Manager() as mgr:
# create a dictionary in the server process
mydict = mgr.dict({100: "Maths", 200: "Science"})
p1 = Process(target=insert_data, args=(mydict, 300, "English"))
p2 = Process(target=insert_data, args=(mydict, 400, "French"))
p3 = Process(target=output, args=(mydict,))
p1.start()
p2.start()
p1.join()
p2.join()
p3.start()
p3.join()
print("Exiting the main process")
+26
View File
@@ -0,0 +1,26 @@
# process5.py to use queue to exchange data
from multiprocessing import Process
from multiprocessing import Queue
def copy_data (list, myqueue):
for num in list:
myqueue.put(num)
def output(myqueue):
while not myqueue.empty():
print(myqueue.get())
mylist = [2, 5, 7]
myqueue = Queue()
p1 = Process(target=copy_data, args=(mylist, myqueue))
p2 = Process(target=output, args=(myqueue,))
p1.start()
p1.join()
p2.start()
p2.join()
print("Queue is empty: ",myqueue.empty())
print("Exiting the main process")
+29
View File
@@ -0,0 +1,29 @@
# process6.py to use Pipe to exchange data
from multiprocessing import Process, Pipe
def mysender (s_conn):
s_conn.send({100: "Maths"})
s_conn.send({200: "Science"})
s_conn.send("BYE")
s_conn.close()
def myreceiver(r_conn):
while True:
msg = r_conn.recv()
if msg == "BYE":
break
print("Received message : ", msg)
r_conn.close()
sender_conn, receiver_conn= Pipe()
p1 = Process(target=mysender, args=(sender_conn, ))
p2 = Process(target=myreceiver, args=(receiver_conn,))
p1.start()
p2.start()
p1.join()
p2.join()
print("Exiting the main process")
+18
View File
@@ -0,0 +1,18 @@
# process7.py to show synchronization and locking
from functools import partial
from multiprocessing import Pool, Manager
def printme (lock, msg):
lock.acquire()
try:
print(msg)
finally:
lock.release()
with Pool(3) as proc:
lock = Manager().Lock()
func = partial(printme,lock)
proc.map(func, ["Orange", "Apple", "Banana",
"Grapes","Pears"])
print("Exiting the main process")
@@ -0,0 +1,55 @@
#processes_casestudy.py
import time
from multiprocessing import Process, JoinableQueue
from getfilelistpy import getfilelist
import gdown
PROCESSES_POOL_SIZE = 5
#update the resource object as per your API key and the gdriver folder id
resource = {
"api_key": "AIzaSyDYKmm85keqnk4bDpYa2bxddKrGns4z0",
"id": "0B8TxHW2Ci6dbckVweTRtV3RUU",
"fields": "files(name,id,webContentLink)",
}
def mydownloader( queue):
while True:
# Get the file id and name from the queue
item1 = queue.get()
try:
gdown.download(item1['webContentLink'],
'./files/{}'.format(item1['name']),
quiet=False)
finally:
queue.task_done()
def get_files(resource):
res = getfilelist.GetFileList(resource)
files_list = res['fileList'][0]
return files_list
def main ():
files = get_files(resource)
#add files info into the queue
myqueue = JoinableQueue()
for item in files['files']:
myqueue.put(item)
processes = []
for id in range(PROCESSES_POOL_SIZE):
p = Process(target=mydownloader,
args=(myqueue,))
p.daemon = True
p.start()
start_time = time.monotonic()
myqueue.join()
total_exec_time = time.monotonic() - start_time
print(f'Time taken to download: {total_exec_time:.2f} seconds')
main()