Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch program with python on a specific core

Is there any way to launch a program by its API using Python and run it on a specified core?

I need to launch a CPU-expensive application about 5 times and run it on different cores to save time.

I'm using windows.

like image 587
TuoCuggino Avatar asked Jul 27 '26 18:07

TuoCuggino


1 Answers

The process can set its own affinity, here is what I do (you can change the mask, or make it a parameter, to identify which cores you want to use.)

import win32api, win32con, win32process

def setaffinity():
    pid  = win32api.GetCurrentProcessId()
    mask = 128 # core 7
    handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
    win32process.SetProcessAffinityMask(handle, mask)
like image 77
Marichyasana Avatar answered Jul 29 '26 06:07

Marichyasana



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!