Process (create, start, join)
Basic Process usage
basic_process.py
from multiprocessing import Process
def work(name: str) -> None:
print("Working in process:", name)
if __name__ == "__main__":
p = Process(target=work, args=("p1",))
p.start()
p.join()
print("Main process finished")basic_process.py
from multiprocessing import Process
def work(name: str) -> None:
print("Working in process:", name)
if __name__ == "__main__":
p = Process(target=work, args=("p1",))
p.start()
p.join()
print("Main process finished")Why join() matters
join()join() waits for the process to finish.
Without it:
- your program might exit early
- you might read results before they are ready
๐งช Try It Yourself
Exercise 1 โ Start a Process
Exercise 2 โ Process Pool map()
Exercise 3 โ Multiprocessing Queue
If this helped you, consider buying me a coffee โ
Buy me a coffeeWas this page helpful?
Let us know how we did
