How to make full use of CPU cores with threading in python 2

前端 未结 2 1281
挽巷
挽巷 2021-01-24 19:07

The following code seems to be executed sequentially rather than concurrently. And it only made use of one CPU core. Is there a way to make it use multiple cores or switch cont

2条回答
  •  旧巷少年郎
    2021-01-24 19:33

    Threads cannot utilize multiple cores in Python. Processes however can.

    multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

    Click here for more information

提交回复
热议问题