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 the portion of the code that will be executed in a subprocess by implementing the Process.run
method. In the following code, we define a Process
class that will wait for one second and print its assigned id
:
import multiprocessing import time class Process(multiprocessing.Process): def __init__(self, id): super(Process, self).__init__() self.id = id def run(self): time.sleep(1) print("I'm the process with id: {}".format(self.id))
To spawn the...