Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify a core to run a python script on. systemd

I have this systemd service:

[Unit]
Description=Toasty
After=multi-user.target

[Service]
WorkingDirectory=/mnt/data/Toasty
User=root
Group=root
ExecStart=/usr/local/bin/python3.5 /mnt/data/Toasty/run.py
Type=idle
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target

What i want is the ability to specify a core for my python code to run on.


1 Answers

What about systemd CPUAffinity:

CPUAffinity=

Configures the CPU affinity for the service manager as well as the default CPU affinity for all forked off processes. Takes a list of CPU indices or ranges separated by either whitespace or commas. CPU ranges are specified by the lower and upper CPU indices separated by a dash. This option may be specified more than once, in which case the specified CPU affinity masks are merged. If the empty string is assigned, the mask is reset, all assignments prior to this will have no effect. Individual services may override the CPU affinity for their processes with the CPUAffinity= setting in unit files, see systemd.exec(5).

Processes under the following service will use logical CPU cores 0 to 20 and 288 to 308:

[Unit]
Description=Toasty
After=multi-user.target

[Service]
WorkingDirectory=/mnt/data/Toasty
User=root
Group=root
ExecStart=/usr/local/bin/python3.5 /mnt/data/Toasty/run.py
CPUAffinity=0-20,288-308
Type=idle
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target
like image 157
hpaknia Avatar answered Dec 30 '25 20:12

hpaknia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!