Using multiple processes
The standard multiprocessing
module can be used to quickly parallelize simple tasks by spawning several processes while avoiding the GIL problem. Its interface is easy to use and includes several utilities to handle task submission and synchronization.
The Process and Pool classes
You can create a process that runs independently by subclassing multiprocessing.Process
. You can extend the __init__
method to initialize resources, and you can write a portion of the code that will be executed in a subprocess by implementing the Process.run
method. In the following code snippet, we define a Process
class that will wait for 1 second and print its assigned id
value:
import multiprocessing import time class Process(multiprocessing.Process): def __init__(self, id): ...