Issue
I know that Python programs execute as a single process using a single CPU.
Does this mean that opening 4 command prompts and launching, one after the other, 4 different .py scripts will result in making use of 4 CPU cores?
My system:
- Alienware Workstation - Intel(R) Core(TM) i7-7700K CPU @ 4.20 GHz
- Windows 10 Home Edition
- Python 2.7.15
Solution
In general, you're right: you'll use one CPU core with one python
process. However, there are many ways which allow you to use more than one CPU core. Have a look at the official Python docs about multiprocessing
.
This is an example, which will stress your CPU on all its cores:
from multiprocessing import Pool, cpu_count
def random_calculation(x):
while True:
x * x
p = Pool(processes=cpu_count())
p.map(random_calculation, range(cpu_count()))
Answered By - finefoot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.