Process class constructor - Python multiprocessing

A process instance can be created by calling the Process class constructor of Python multiprocessing package. Calling start method on the returned process instance makes the new process running inside the operating system

Process Updated 2 Feb 2019 1 min read

Method Name

Process

Method Signature

Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None))

Return Value

Returns a Process object.

Method Overview

  • The Process() constructor creates a Process instance.
  • By calling the start() method of this, instance the process is executed/introduced into the operating system.
  • Any required parameters for the communication between the parent process and the child process are provided through args and kwargs.
  • The process can be a normal process or a daemon process based on the Boolean value passed to the daemon named parameter.

Parameters

group – This parameter exists to have compatibility with Threading.Thread class. The value passed should be None.

target – A callable object whose contents/instructions to be executed as a separate process.

Name - Name of the process. This is not a unique value across the list of running processes.

Args – Parameters passed for communication between the parent process and the child process. This is a tuple.

Kwargs - Parameters passed for communication between the parent process and the child process. This is a dictionary.

daemon – A Boolean value based on which the process is created as a Regular Process or a Daemon Process.

Example

python
import multiprocessing as multiproc

def procFunction(mq):

    mq.put("Message from child process")

if __name__ == "__main__":

    multiproc.set_start_method("spawn")

    mq = multiproc.Queue()

    # Send the message queue as argument tuple for target invocation

    childProc  = multiproc.Process(target=procFunction, args=(mq,))

    childProc.start()

    childProc.join()

    # print the message queue

    print(mq.get())

Output

Output
Message from child process