Skip to content

New Process

from multiprocessing import Process

# Create a new process 
process = Process()
# Create a new process with specified function to execute.
def example_function():
    pass

process = Process(target=example_function)
# Create a new process with specific function to execute with args. 
def example(args):
    pass

process = Process(target=example, args=("Hi",))

Running the new process

# Run the new process 
process.start()

# Check process is running
process.is_alive()

The terminated process

# Check process is terminated - should return false.
process.is_alive()
# Create a new process with a specific function that has an error 
# to execute with args. 
def example(args):
    split_args = list(args.split())
    # "name" variable is not in the function namespace - should raise error 
    return name

# New process
process = Process(target=example, args=("Hi",))

# Running the new process
process.start()

# Check process is running
process.is_alive()
# Create a new process with a specific function to execute with args. 
def example(args):
    split_args = list(args.split())
    # "name" variable is not in the function namespace - should raise error 
    return split_args

# New process
process = Process(target=example, args=("Hi",))

# Running the new process
process.start()

if process.is_alive():
    process.terminate()
    print("Process terminated forcefully")

Multiprocessing tutorial

import time

def do_something():
    print("I'm going to sleep")
    time.sleep(1)
    print("I'm awake") 
# Create new child process
process_1 = Process(target=do_something)
process_2 = Process(target=do_something)
%%time 

# Starts both processes
process_1.start()
process_2.start()
%%time 

# Create new child process (Cannot run a process more than once)
new_process_1 = Process(target=do_something)
new_process_2 = Process(target=do_something)

# Starts both processes
new_process_1.start()
new_process_2.start()

new_process_1.join()
new_process_2.join()