MacOS: How to set Python 3 on process affinity

MacOS: How to set Python 3 on process affinity … here is a solution to the problem.

MacOS: How to set Python 3 on process affinity

I’m trying to limit the number of CPUs Python uses (for benchmarking and seeing if it speeds up my program).

I’ve found some Python modules (‘os’, ‘affinity’, ‘psutil’) for this purpose, except that the way they change affinity only works for Linux (and sometimes Windows). There is also a suggestion to use the “taskset” command (Why does multiprocessing use only a single core after I import numpy?), but as far as I know, this command is not available on macOS.

When running Python/iPython on macOS, is there a (preferably clean and simple) way to change the affinity? Changing processor affinity in Mac doesn’t seem as easy as it is in other platforms ( http://softwareramblings.com/2008/04/thread-affinity-on-os-x.html)。

Solution

No way. See also Thread Affinity API Release Notes:

OS X does not export interfaces that identify processors or control thread placement—explicit thread to processor binding is not supported. Instead, the kernel manages all thread placement. Applications expect that the scheduler will, under most circumstances, run its threads using a good processor placement with respect to cache affinity.

Note that thread affinity is something you think about fairly late when optimizing your program, there are millions of things to do that have a greater impact on your program.

Also note that Python was particularly bad at multithreading in the first place.

Related Problems and Solutions