Python 3 on macOS: how to set process affinity
Asked Answered
C

1

6

I am trying to restrict the number of CPUs used by Python (for benchmarking & to see if it speeds up my program).

I have found a few Python modules for achieving this ('os', 'affinity', 'psutil') except that their methods for changing affinity only works with 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 this command not available on macOS as far as I know.

Is there a (preferable clean & easy) way to change affinity while running Python / iPython on macOS? It seems like changing processor affinity in Mac is not as easy as in other platforms (http://softwareramblings.com/2008/04/thread-affinity-on-os-x.html).

Compact answered 7/3, 2017 at 21:6 Comment(0)
D
5

Not possible. See 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'd consider fairly late when optimizing a program, there are a million things to do which have a larger impact on your program.

Also note that Python is particularly bad at multithreading to begin with.

Delciedelcina answered 7/3, 2017 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.