Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Interprocess Communication and Thread Coding: I have finished the coding part of the assignment. The .py file as well as the output are attached: Questions:

Interprocess Communication and Thread Coding:

I have finished the coding part of the assignment. The .py file as well as the output are attached:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Questions:

  1. Assuming your program properly executed to completion, why were there some instances of pauses of several seconds between output messages?
  2. Given that each of the five threads were spawned within the space of a second, or two, why did they reach completion at considerably (within the scope of the exercise) different times?
4 # Example of a process spawning multiple threads 5 # Also illustrates Asynchronous communication 7 import threading 8 import datetime 9 import time 10 import random 12 number of iterations10 13 max-delay- 10 14 15 def thread1(): 16 17 18 19 20 21 delay 0 for i in range (number_of iterations): timeR datetime.datetime.now() print Thread1-' + str(i) + '' Delay: delay = random . randrange (max-delay) time.sleep(delay) + str(delay) + seconds. Time : + str(tineR)) 23 24 25 def thread2): 26 27 28 29 30 31 32 delay = 0 for i in range(number_of iterations): timeR datetime.datetime.now() print( Thread2-' + str() + '' Delay: delay - random.randrange (max_delay) time.sleep(delay) + str(delay) + seconds. Time: + str(tineR)) 34 35 def thread3): 36 37 38 39 40 41 42 43 delay 0 for i in range (numberof iterations): timeR datetime.datetime.now() print('Thread3- str(i)+ ', Delay: '+ str(delay) seconds. Time: '+ str(timeR)) delay - random.randrange (max_delay) time.sleep (delay) 45 def thread4(): 47 48 49 50 51 52 53 54 55 def thread5): 56 57 58 59 60 61 62 63 64 65 66 def main): 67 68 69 70 71 72 73 74 75 76 delay for i in range(number_of_iterations): timeRdatetime.datetime.now() print('Thread4-'+ str(i)+ ', Delay: 'str(delay)seconds. Time:'str(timeR)) delay - random.randrange (max_delay) time.sleep(delay) delay = 0 for i in range(number_of_iterations): timeRdatetime.datetime.now() print('Thread5-'+ str(i)+ ', Delay: 'str(delay)seconds. Time:'str(timeR)) delay - random.randrange (max_delay) time.sleep(delay) start-time datetime.datetime . now()| print('BEGIN:' + str(start time)) t1 - threading.Thread(target -thread1) t2 - threading. Thread(target- thread2) t3 - threading. Thread(target- thread3) t4 - threading.Thread(target thread4) t5 - threading.Thread(target thread5) t1.start) t2.start() t3.start() t4.start() t5.start() 78 79 80 81 82 83 84 85 86 87 #In Python's threading package, the join() operator is used to close a thread #(By [re-]joining the thread with it's parent process upon completion) t1.join() t2.join() t3.join() t4.join() t5.join) t1.start() t2.start() t3.start() t4.start() t5.start() 75 76 78 79 80 81 82 83 84 85 86 87 #In Python's threading package, the join() operator is used to close a thread #(By [re-joining the thread with It's parent process upon completion) t1.join) t2.join) t3.join) t4.join) t5.join) end_time - datetime.datetime.now() print('END:'+ str(end_time)) 89 90 91 92 if 93 94 95 -_name main() _main . BEGIN: 2019-02-03 03:31:52.094841 Thread1-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095165 Thread2-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095322 Thread3-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095447 Thread4-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095600 [Thread3-1, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095625 Thread5-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095778 Thread4-1, Delay: 2 seconds. Time: 2019-02-03 03:31:54.096569 Thread1-1, Delay: 3 seconds. Time: 2019-02-03 03:31:55.100790 Thread4-2, Delay: 4 seconds. Time: 2019-02-03 03:31:58.100779 Thread2-1, Delay: 6 seconds. Time: 2019-02-03 03:31:58.100907 Thread5-1, Delay: 7 seconds. Time: 2019-02-03 03:31:59.101328 Thread3-2, Delay: 9 seconds. Time: 2019-02-03 03:32:01.101399 Thread3-3, Delay 0 seconds. Time: 2019-02-03 03:32:01.101545 Thread4-3, Delay: 3 seconds. Time: 2019-02-03 03:32:01.101597 Thread1-2, Delay: 7 seconds. Time: 2019-02-03 03:32:02.106469 Thread3-4, Delay: 2 seconds. Time: 2619-02-03 03:32:03.107157 Thread2-2, Delay: 7 seconds. Time: 2019-02-03 03:32:05.106638 Thread5-2, Delay: 6 seconds. Time: 2019-02-03 03:32:05.186793 Thread5-3, Delay: 2 seconds. Time: 2019-02-03 03:32:07.109411 Thread2-3, Delay: 2 seconds. Time: 2019-02-03 03:32:07.109513 Thread4-4, Delay 7 seconds. Time: 2019-02-03 03:32:08.107285 Thread3-5, Delay: 5 seconds. Time: 2019-02-03 03:32:08.107432 Thread4-5, Delay: 2 seconds. Time: 2019-02-03 03:32:10.112872 Thread1-3, Delay: 9 seconds. Time: 2019-02-03 03:32:11.112168 Thread5-4, Delay: 5 seconds. Time: 2019-02-03 03:32:12.110117 Thread2-4, Delay: 8 seconds. Time: 2019-02-03 03:32:15.113137 Thread3-6, Delay: 9 seconds. Time: 2019-02-03 03:32:17.110380 Thread5-5, Delay: 6 seconds. Time: 2019-02-03 03:32:18.110826 Thread4-6, Delay: 8 seconds. Time: 2019-02-03 03:32:18.113478 Thread3-7, Delay: 2 seconds. Time: 2019-02-03 03:32:19.114129 Thread1-4, Delay: 8 seconds Thread5-6, Delay: 3 Thread3-8, Delay: 2 seconds. Time: 2019-02-03 03:32:21.114370 Thread2-5, Delay: 7 seconds. Time: 2619-02-03 03:32:22.116270 Thread4-7, Delay: 5 seconds. Time: 2019-02-03 03:32:23.118602 Thread4-8, Delay: 0 seconds. Time: 2019-02-03 03:32:23.118740 Thread2-6, Delay: 2 s Thread4-9, Delay: 2 seconds. Time: 2019-02-03 03:32:25.123892 Thread1-5, Delay 7 seconds. Time: 2019-02-03 03:32:26.118678 Thread3-9, Delay: 8 seconds. Time: 2019-02-03 03:32:29.119426 Thread5-7, Delay: 9 seconds. Time: 2019-02-03 03:32:30.114297 Thread1-6, Delay: 4 seconds. Time: 2019-02-03 03:32:30.122067 Thread2-7, Delay: 6 seconds. Time: 2019-02-03 03:32:30.122299 Thread1-7, Delay: 3 seconds. Time: 2019-02-03 03:32:33.122619 Thread2-8, Delay: 4 seconds. Time: 2019-02-03 03:32:34.126031 Thread2-9, Delay: 0 seconds. Time: 2019-02-03 03:32:34.126199 Thread1-8, Delay: 4 seconds. Time: 2019-02-03 03:32:37.124852 Thread5-8, Delay: 9 seconds. Time: 2019-02-03 03:32:39.116684 Thread5-9, Delay: 6 seconds. Time: 2019-02-03 03:32:45.118606 Thread1-9, Delay: 9 seconds. Time: 2019-02-03 03:32:46.130269 END: 2019-02-03 03:32:50.121468 me: 2019-02-03 03:32:19.114279 me: 2019-02-03 03:32:21.113885 econds. Ti me: 2019-02-03 03:32:24.121058 4 # Example of a process spawning multiple threads 5 # Also illustrates Asynchronous communication 7 import threading 8 import datetime 9 import time 10 import random 12 number of iterations10 13 max-delay- 10 14 15 def thread1(): 16 17 18 19 20 21 delay 0 for i in range (number_of iterations): timeR datetime.datetime.now() print Thread1-' + str(i) + '' Delay: delay = random . randrange (max-delay) time.sleep(delay) + str(delay) + seconds. Time : + str(tineR)) 23 24 25 def thread2): 26 27 28 29 30 31 32 delay = 0 for i in range(number_of iterations): timeR datetime.datetime.now() print( Thread2-' + str() + '' Delay: delay - random.randrange (max_delay) time.sleep(delay) + str(delay) + seconds. Time: + str(tineR)) 34 35 def thread3): 36 37 38 39 40 41 42 43 delay 0 for i in range (numberof iterations): timeR datetime.datetime.now() print('Thread3- str(i)+ ', Delay: '+ str(delay) seconds. Time: '+ str(timeR)) delay - random.randrange (max_delay) time.sleep (delay) 45 def thread4(): 47 48 49 50 51 52 53 54 55 def thread5): 56 57 58 59 60 61 62 63 64 65 66 def main): 67 68 69 70 71 72 73 74 75 76 delay for i in range(number_of_iterations): timeRdatetime.datetime.now() print('Thread4-'+ str(i)+ ', Delay: 'str(delay)seconds. Time:'str(timeR)) delay - random.randrange (max_delay) time.sleep(delay) delay = 0 for i in range(number_of_iterations): timeRdatetime.datetime.now() print('Thread5-'+ str(i)+ ', Delay: 'str(delay)seconds. Time:'str(timeR)) delay - random.randrange (max_delay) time.sleep(delay) start-time datetime.datetime . now()| print('BEGIN:' + str(start time)) t1 - threading.Thread(target -thread1) t2 - threading. Thread(target- thread2) t3 - threading. Thread(target- thread3) t4 - threading.Thread(target thread4) t5 - threading.Thread(target thread5) t1.start) t2.start() t3.start() t4.start() t5.start() 78 79 80 81 82 83 84 85 86 87 #In Python's threading package, the join() operator is used to close a thread #(By [re-]joining the thread with it's parent process upon completion) t1.join() t2.join() t3.join() t4.join() t5.join) t1.start() t2.start() t3.start() t4.start() t5.start() 75 76 78 79 80 81 82 83 84 85 86 87 #In Python's threading package, the join() operator is used to close a thread #(By [re-joining the thread with It's parent process upon completion) t1.join) t2.join) t3.join) t4.join) t5.join) end_time - datetime.datetime.now() print('END:'+ str(end_time)) 89 90 91 92 if 93 94 95 -_name main() _main . BEGIN: 2019-02-03 03:31:52.094841 Thread1-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095165 Thread2-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095322 Thread3-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095447 Thread4-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095600 [Thread3-1, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095625 Thread5-0, Delay: 0 seconds. Time: 2019-02-03 03:31:52.095778 Thread4-1, Delay: 2 seconds. Time: 2019-02-03 03:31:54.096569 Thread1-1, Delay: 3 seconds. Time: 2019-02-03 03:31:55.100790 Thread4-2, Delay: 4 seconds. Time: 2019-02-03 03:31:58.100779 Thread2-1, Delay: 6 seconds. Time: 2019-02-03 03:31:58.100907 Thread5-1, Delay: 7 seconds. Time: 2019-02-03 03:31:59.101328 Thread3-2, Delay: 9 seconds. Time: 2019-02-03 03:32:01.101399 Thread3-3, Delay 0 seconds. Time: 2019-02-03 03:32:01.101545 Thread4-3, Delay: 3 seconds. Time: 2019-02-03 03:32:01.101597 Thread1-2, Delay: 7 seconds. Time: 2019-02-03 03:32:02.106469 Thread3-4, Delay: 2 seconds. Time: 2619-02-03 03:32:03.107157 Thread2-2, Delay: 7 seconds. Time: 2019-02-03 03:32:05.106638 Thread5-2, Delay: 6 seconds. Time: 2019-02-03 03:32:05.186793 Thread5-3, Delay: 2 seconds. Time: 2019-02-03 03:32:07.109411 Thread2-3, Delay: 2 seconds. Time: 2019-02-03 03:32:07.109513 Thread4-4, Delay 7 seconds. Time: 2019-02-03 03:32:08.107285 Thread3-5, Delay: 5 seconds. Time: 2019-02-03 03:32:08.107432 Thread4-5, Delay: 2 seconds. Time: 2019-02-03 03:32:10.112872 Thread1-3, Delay: 9 seconds. Time: 2019-02-03 03:32:11.112168 Thread5-4, Delay: 5 seconds. Time: 2019-02-03 03:32:12.110117 Thread2-4, Delay: 8 seconds. Time: 2019-02-03 03:32:15.113137 Thread3-6, Delay: 9 seconds. Time: 2019-02-03 03:32:17.110380 Thread5-5, Delay: 6 seconds. Time: 2019-02-03 03:32:18.110826 Thread4-6, Delay: 8 seconds. Time: 2019-02-03 03:32:18.113478 Thread3-7, Delay: 2 seconds. Time: 2019-02-03 03:32:19.114129 Thread1-4, Delay: 8 seconds Thread5-6, Delay: 3 Thread3-8, Delay: 2 seconds. Time: 2019-02-03 03:32:21.114370 Thread2-5, Delay: 7 seconds. Time: 2619-02-03 03:32:22.116270 Thread4-7, Delay: 5 seconds. Time: 2019-02-03 03:32:23.118602 Thread4-8, Delay: 0 seconds. Time: 2019-02-03 03:32:23.118740 Thread2-6, Delay: 2 s Thread4-9, Delay: 2 seconds. Time: 2019-02-03 03:32:25.123892 Thread1-5, Delay 7 seconds. Time: 2019-02-03 03:32:26.118678 Thread3-9, Delay: 8 seconds. Time: 2019-02-03 03:32:29.119426 Thread5-7, Delay: 9 seconds. Time: 2019-02-03 03:32:30.114297 Thread1-6, Delay: 4 seconds. Time: 2019-02-03 03:32:30.122067 Thread2-7, Delay: 6 seconds. Time: 2019-02-03 03:32:30.122299 Thread1-7, Delay: 3 seconds. Time: 2019-02-03 03:32:33.122619 Thread2-8, Delay: 4 seconds. Time: 2019-02-03 03:32:34.126031 Thread2-9, Delay: 0 seconds. Time: 2019-02-03 03:32:34.126199 Thread1-8, Delay: 4 seconds. Time: 2019-02-03 03:32:37.124852 Thread5-8, Delay: 9 seconds. Time: 2019-02-03 03:32:39.116684 Thread5-9, Delay: 6 seconds. Time: 2019-02-03 03:32:45.118606 Thread1-9, Delay: 9 seconds. Time: 2019-02-03 03:32:46.130269 END: 2019-02-03 03:32:50.121468 me: 2019-02-03 03:32:19.114279 me: 2019-02-03 03:32:21.113885 econds. Ti me: 2019-02-03 03:32:24.121058

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

Identify and describe basic workplace competencies

Answered: 1 week ago

Question

Describe the steps involved in coaching to improve poor performance

Answered: 1 week ago