Quiz 1
Understand how Python's multiprocessing module affects lock objects and process execution. Learn about different start methods like fork and spawn, and how RLock behaves differently when used with multiprocessing versus threading across multiple processes. This lesson helps you grasp process isolation and synchronization challenges in Python concurrency.
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()
What will be the output of all the print statements?
Exactly the same
All are different
They may be different
Question # 2
Consider the same code from the previous question and assume we change the start method from fork to spawn.
Will the output of the print statements be same?
Yes
No
Maybe
Observe that if the start method is changed to forkserver then the id for str_obj variable will be the same for all child ...