How to pin different processes to individual cpu cores in Python

≯℡__Kan透↙ 提交于 2020-03-21 05:49:50

问题


I'm writing a Python program that will load a wordlist from a text file and then try unzipping an archive with each word. It wouldn't be serious if it didn't make use of all cpu cores. Because of the GIL, threading in Python isn't a great option if I'm not mistaken.

So I want to get the number of cpu_cores, split the wordlist and use the multiprocessing.process module to process different parts of the wordlist in different processes.

But would every process get pinned to a cpu core automatically? If not, is there a way to pin them manually?


回答1:


You can use Pythons multiprocessing by importing import multiprocessing as mp and find out the number of processors by using mp.cpu_count() and should work on most platforms.

To launch programs/processes on specific CPU cores (in linux) you can use taskset and use this guide as a reference.

An alternative cross-plattform solution would be to use the psutil package for python.

However i would suggest you go with a thread/process pooling approach as in my opinion you should let the operating system assign tasks to each cpu/core. You can look at How to utilize all cores with python multiprocessing on how to approach this problem.



来源:https://stackoverflow.com/questions/43538141/how-to-pin-different-processes-to-individual-cpu-cores-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!