Quiz 1
Test what you have learnt so far.
We'll cover the following...
Question # 1
Consider the snippet below:
def child_task(str_obj):
print(str_obj.__hash__())
if __name__ == '__main__':
str_obj = "Educative!"
multiprocessing.set_start_method('fork')
print(str_obj.__hash__())
process1 = Process(target=child_task, args=(str_obj,))
process1.start()
process2 = Process(target=child_task, args=(str_obj,))
process2.start()
process1.join()
process2.join()
Q
What will be the output of all the print statements?
A)
Exactly the same
B)
All are different
C)
They may be different
Press + to interact
from multiprocessing import Process, current_processimport multiprocessingdef child_task(str_obj):print(id(str_obj))if __name__ == '__main__':str_obj = "Educative!"multiprocessing.set_start_method('fork')print(id(str_obj))process1 = Process(target=child_task, args=(str_obj,))process1.start()process2 = Process(target=child_task, args=(str_obj,))process2.start()process1.join()process2.join()
Question # 2
Consider the same code from the previous question and assume we change the start method from fork to spawn.
Q
Will the output of the print statements be same?
A)
Yes
B)
No
C)
Maybe
Press + to interact
from multiprocessing import Process, current_processimport multiprocessingdef child_task(str_obj):print(id(str_obj))if __name__ == '__main__':str_obj = "Educative!"multiprocessing.set_start_method('spawn')print(id(str_obj))process1 = Process(target=child_task, args=(str_obj,))process1.start()process2 = Process(target=child_task, args=(str_obj,))process2.start()process1.join()process2.join()
Observe that if the start method is changed to forkserver then the id for str_obj
variable will be the same ...