6

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
2
  • 2
    You should take a look at python multiprocessor Commented Dec 11, 2018 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. Commented Dec 11, 2018 at 11:12

1 Answer 1

6

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()))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.