python multiprocessing - ghdrako/doc_snipets GitHub Wiki
#!/usr/bin/env python
import os
# A very, very simple process.
if __name__ == "__main__":
print(f"Hi! I'm process {os.getpid()}")
Creating a child process from your parent
#!/usr/bin/env python
import os
import multiprocessing
def child_process():
print(f"Hi! I'm a child process {os.getpid()}")
if __name__ == "__main__":
print(f"Hi! I'm process {os.getpid()}")
# Here we create a new instance of the Process class and assign our
# `child_process` function to be executed.
process = multiprocessing.Process(target=child_process)
# We then start the process
process.start()
# And finally, we join the process. This will make our script to hang and
# wait until the child process is done.
process.join()
if you don't use the process.join() to wait for your child process to execute and finish, then any other subsequent code that point will actually execute and may become a bit harder to synchronize your workflow.
Creating various child processes from a parent process
#!/usr/bin/env python
import os
import multiprocessing
def child_process(id):
print(f"Hi! I'm a child process {os.getpid()} with id#{id}")
if __name__ == "__main__":
print(f"Hi! I'm process {os.getpid()}")
list_of_processes = []
# Loop through the number 0 to 10 and create processes for each one of
# them.
for i in range(0, 10):
# Here we create a new instance of the Process class and assign our
# `child_process` function to be executed. Note the difference now that
# we are using the `args` parameter now, this means that we can pass
# down parameters to the function being executed as a child process.
process = multiprocessing.Process(target=child_process, args=(i,))
list_of_processes.append(process)
for process in list_of_processes:
# We then start the process
process.start()
# And finally, we join the process. This will make our script to hang
# and wait until the child process is done.
process.join()
Communicating data between child process and parent process
we can use the queue class, which will provide us an interface to communicate data between the parent process and its child processes. A queue, in this context, is a normal FIFO (First In First Out) that has a built-in mechanism for working with multiprocessing
#!/usr/bin/env python
import os
import multiprocessing
def child_process(queue, number1, number2):
print(f"Hi! I'm a child process {os.getpid()}. I do calculations.")
sum = number1 + number2
# Putting data into the queue
queue.put(sum)
if __name__ == "__main__":
print(f"Hi! I'm process {os.getpid()}")
# Defining a new Queue()
queue = multiprocessing.Queue()
# Here we create a new instance of the Process class and assign our
# `child_process` function to be executed. Note the difference now that
# we are using the `args` parameter now, this means that we can pass
# down parameters to the function being executed as a child process.
process = multiprocessing.Process(target=child_process, args=(queue,1, 2))
# We then start the process
process.start()
# And finally, we join the process. This will make our script to hang and
# wait until the child process is done.
process.join()
# Accessing the result from the queue.
print(f"Got the result from child process as {queue.get()}")