The process class

In the multiprocessing module, processes are typically spawned and managed through the Process class. Each Process object represents an activity that executes in a separate process. Conveniently, the Process class has equivalent methods and APIs that can be found in the threading.Thread class.

Specifically, utilizing an object-oriented programming approach, the Process class from multiprocessing provides the following resources:

  • run(): This method is executed when a new process is initialized and started
  • start(): This method starts the initialized calling Process object by calling the run() method
  • join(): This method waits for the calling Process object to terminate before continuing with the execution of the rest of the program
  • isAlive(): This method returns a Boolean value indicating whether the calling Process object is currently executing
  • name: This attribute contains the name of the calling Process object
  • pid: This attribute contains the process ID of the calling Process object
  • terminate(): This method terminates the calling Process object

As you can see from our previous example, while initializing a Process object, we can pass parameters to a function and execute it in a separate process by specifying the target (for the target function) and args (for target function arguments) parameters. Note that one could also override the default Process() constructor and implement one's own run() function.

As it is a major player in the multiprocessing module and in concurrency in Python in general, we will look at the Process class again in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.249.220