Using Python multiprocessing.Process inside a Docker Container

Viewed 549

The code below works when being run as a regular Python process. Calling start starts a new process.
When I run the same code inside a docker container, the method my_function never gets called.

from multiprocessing import Process

def my_function(x, y):
    print("process started")


def start():
    p = Process(target=my_function, args=(1, 2))
    p.start()

The Dockerfile has FROM python:3.8-slim-buster.
Is there some configuration needed on the Docker side to enable the multiprocessing?

2 Answers

Make sure you have the Dockerfile in a valid format. Your code worked with my valid Dockerfile and printed process started as expected.

You can compare with the following the example of both Dockerfile and the python script file (prints the process id to validate) below..

Example below:

FROM python:3.8-slim-buster

ADD multiprocess_test.py /
CMD [ "python", "./multiprocess_test.py" ]

I have the following codes, in multiprocess_test.py source: official documenation:

from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

Now, do docker build -t mp-test . and then docker run --rm -it mp-test:latest, the result would be like

main line
module name: __main__
parent process: 0
process id: 1
function f
module name: __main__
parent process: 1
process id: 6
hello bob
Related