Search⌘ K

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()
Technical Quiz
1.

What will be the output of all the print statements?

A.

Exactly the same

B.

All are different

C.

They may be different


1 / 1
Python 3.5
from multiprocessing import Process, current_process
import multiprocessing
def 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.

Technical Quiz
1.

Will the output of the print statements be same?

A.

Yes

B.

No

C.

Maybe


1 / 1
Python 3.5
from multiprocessing import Process, current_process
import multiprocessing
def 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 for all child ...