Search code examples
pythonunixcpu-cores

How to run a Python program ona specific CPU core


Say I have a python file - file1.py with arg --in

python file1.py --in 10

I have an 8-core Unix machine, I want to run the same script 8 times (infinite loop) (diff --in value) which should run in 8 different cores, like

python file1.py --in 10 --> in core 1

python file1.py --in 20 --> in core 2

python file1.py --in 130 --> in core 3

.. so on

How to implement this? How do I specify which core to run?

python file1.py --in 10 &

python file1.py --in 20 &

python file1.py --in 130 &

If I run it like this, will it run on the same core or different cores?


Solution

  • Try taskset

    taskset -c 1 python file1.py --in 10
    taskset -c 2 python file1.py --in 20
    taskset -c 3 python file1.py --in 130