Skip to content

Multiprocessing Not working in EC2

0

I have a python fast api application in which i have multiprocessing (parallel Processing) code. The application is working in my local Ubuntu server as expected. But when i deploy the same as docker image in my EC2 (c6i-xlarge) instance (Ubuntu OS), it is not evening triggering that code. Are there any setting i need to do for multiprocessing to work in EC2 machine?

Thanks

asked 2 years ago416 views
1 Answer
0

Multiprocessing is a part of python standard library , you don't need to make any changes to ec2 instance setting, can you also post errors if you encountered any?

I was able to test using the same ec2 instance type c6i.xlarge please see the code below

running the python file directly

python3 mptest.py 
[1, 4, 9]
[ec2-user@ip-172-31-18-12 python-docker]$ 

I previously built the docker container using sudo docker build -t python-imagename . and here is the docker run output

 sudo docker run python-imagename
[1, 4, 9]

this is the file I use from referenced python documentation

[ec2-user@ip-172-31-18-12 python-docker]$ cat mptest.py 
from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))

this is my dockerfile

cat dockerfile 
FROM python:3.9 
# Or any preferred Python version.
ADD mptest.py .
CMD ["python3", "./mptest.py"] 

HTH

Thanks

AWS
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.