Designate specific CPU for a process - python multiprocessing

10,301

Solution 1

It's not something Python does out of the box. Is also somewhat OS-specific. See this answer on doing under Linux: https://stackoverflow.com/a/9079117/4822566

Solution 2

I just had the similar problem on the project with 3 servers/dispatchers running each on its own CPU core, spawning highly CPU intensive workers - which, at the same time, were designed to use specific core(s) they were running on to its fullest potential.

I needed to assure that such worker process would never be spawned on (or used - as some of the workers also used multiprocessing) the CPU core running the server/dispatcher as this caused problem with operation of the later.

I used the following code from Ioannis Filippidis's answer and it is working great for limiting any process to any core(s). I used multiprocessing pool in this example, however code from the child works in any multiprocessing.Process. Note: it does not work on macOS.

import multiprocessing as mp


def child(worker: int) -> None:
    import psutil
    import time

    p = psutil.Process()
    print(f"Child #{worker}: {p}, affinity {p.cpu_affinity()}", flush=True)
    time.sleep(1)
    p.cpu_affinity([worker])
    print(f"Child #{worker}: Set my affinity to {worker}, affinity now {p.cpu_affinity()}", flush=True)

    time.sleep(1 + 3 * worker)
    print(f"Child #{worker}: Starting CPU intensive task now for 4 seconds on {p.cpu_affinity()}...", flush=True)
    t_end = time.perf_counter() + 4
    while time.perf_counter() < t_end:
        pass
    print(f"Child #{worker}: Finished CPU intensive task on {p.cpu_affinity()}", flush=True)


def main() -> None:
    with mp.Pool() as pool:
        # noinspection PyProtectedMember
        workers: int = pool._processes
        print(f"Running pool with {workers} workers")

        for i in range(workers):
            pool.apply_async(child, (i,))

        # Wait for children to finnish
        pool.close()
        pool.join()

    pass


if __name__ == '__main__':
    main()

Output in the console:

Running pool with 16 workers
Child #0: psutil.Process(pid=16168, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #1: psutil.Process(pid=20864, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #2: psutil.Process(pid=15748, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #4: psutil.Process(pid=20600, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #3: psutil.Process(pid=17900, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #5: psutil.Process(pid=3288, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #7: psutil.Process(pid=19308, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #6: psutil.Process(pid=9768, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #8: psutil.Process(pid=1988, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #9: psutil.Process(pid=13960, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #11: psutil.Process(pid=3068, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #10: psutil.Process(pid=9636, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #12: psutil.Process(pid=18608, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #13: psutil.Process(pid=14356, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #14: psutil.Process(pid=14636, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #15: psutil.Process(pid=17372, name='python.exe', started='23:03:09'), affinity [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Child #0: Set my affinity to 0, affinity now [0]
Child #1: Set my affinity to 1, affinity now [1]
Child #2: Set my affinity to 2, affinity now [2]
Child #4: Set my affinity to 4, affinity now [4]
Child #3: Set my affinity to 3, affinity now [3]
Child #5: Set my affinity to 5, affinity now [5]
Child #7: Set my affinity to 7, affinity now [7]
Child #6: Set my affinity to 6, affinity now [6]
Child #8: Set my affinity to 8, affinity now [8]
Child #9: Set my affinity to 9, affinity now [9]
Child #10: Set my affinity to 10, affinity now [10]
Child #11: Set my affinity to 11, affinity now [11]
Child #12: Set my affinity to 12, affinity now [12]
Child #13: Set my affinity to 13, affinity now [13]
Child #14: Set my affinity to 14, affinity now [14]
Child #15: Set my affinity to 15, affinity now [15]
Child #0: Starting CPU intensive task now for 4 seconds on [0]...
Child #1: Starting CPU intensive task now for 4 seconds on [1]...
Child #0: Finished CPU intensive task on [0]
Child #2: Starting CPU intensive task now for 4 seconds on [2]...
Child #1: Finished CPU intensive task on [1]
Child #3: Starting CPU intensive task now for 4 seconds on [3]...
Child #2: Finished CPU intensive task on [2]
Child #4: Starting CPU intensive task now for 4 seconds on [4]...
Child #3: Finished CPU intensive task on [3]
Child #5: Starting CPU intensive task now for 4 seconds on [5]...
Child #4: Finished CPU intensive task on [4]
Child #6: Starting CPU intensive task now for 4 seconds on [6]...
Child #5: Finished CPU intensive task on [5]
Child #7: Starting CPU intensive task now for 4 seconds on [7]...
Child #6: Finished CPU intensive task on [6]
Child #8: Starting CPU intensive task now for 4 seconds on [8]...
Child #7: Finished CPU intensive task on [7]
Child #9: Starting CPU intensive task now for 4 seconds on [9]...
Child #8: Finished CPU intensive task on [8]
Child #10: Starting CPU intensive task now for 4 seconds on [10]...
Child #9: Finished CPU intensive task on [9]
Child #11: Starting CPU intensive task now for 4 seconds on [11]...
Child #10: Finished CPU intensive task on [10]
Child #12: Starting CPU intensive task now for 4 seconds on [12]...
Child #11: Finished CPU intensive task on [11]
Child #13: Starting CPU intensive task now for 4 seconds on [13]...
Child #12: Finished CPU intensive task on [12]
Child #14: Starting CPU intensive task now for 4 seconds on [14]...
Child #13: Finished CPU intensive task on [13]
Child #15: Starting CPU intensive task now for 4 seconds on [15]...
Child #14: Finished CPU intensive task on [14]
Child #15: Finished CPU intensive task on [15]

Process finished with exit code 0

View in the Task Manager: Task Manager - Performance

Share:
10,301
ccdpowell
Author by

ccdpowell

Engineer turned Salesperson turned Entrepreneur turning programmer.

Updated on June 25, 2022

Comments

  • ccdpowell
    ccdpowell almost 2 years

    I am using Redis as my queue for a producer/consumer relationship in a multiprocessing setup.

    My problem is that my producers are overloading my consumer then stealing it's CPU.

    My question, can I allocate an entire processor to specific function/process (IE: the consumer) in this setup.