Learn Before
Code

Multiprocessing Example

Here is a simple example of using multiple processes to print a string.

import multiprocessing def f(): print("Hello World!", multiprocessing.current_process()) if __name__ == '__main__': processes = [multiprocessing.Process(target=f) for x in range(3)] # Creates list of processes -> [proc-1, proc-2, proc-3] for i in processes: i.start() for i in processes: i.join()

The .start() method signals the process to begin execution. Alternatively, the .join() method signals for the process to stop once it is done executing the function. The code above printed:

Hello World! <Process name='Process-2' parent=15444 started> Hello World! <Process name='Process-1' parent=15444 started> Hello World! <Process name='Process-3' parent=15444 started>

0

1

Updated 2021-03-31

Tags

Python Programming Language

Data Science