5

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
finefoot
  • 9,914
  • 7
  • 59
  • 102
Employee
  • 3,109
  • 5
  • 31
  • 50
  • 2
    You should take a look at [python multiprocessor](https://stackoverflow.com/questions/23537037/python-multicore-programming) – tunglt Dec 11 '18 at 11:11
  • yes. those are separate processes and under windows (and most OSs) they will normally be scheduled to run in parallel on multiple cores. – Sam Mason Dec 11 '18 at 11:12

1 Answers1

3

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()))
finefoot
  • 9,914
  • 7
  • 59
  • 102