Jump to content

Does Python use multiple cores?

I had this question in my mind since a long time, I've done much research regarding this but I always end up unsatisfied with the answers.

So, does Python (interpreting) use multiple cores?

I started out Python a few months ago, I really liked the language, but my PC wasn't capable handling the interpreter (it's very old). Thus, I wanted to buy a new PC and this question arose in my mind, as I don't have the budget to buy a PC with i5 8th Gen which has 6 cores, and I rather thought of settling for the i3 8th Gen.

So does it make any difference?

 

Answers would be appreciated.

Thank you.

 

P.S. I am a student9_9

PC

 

CPU : Ryzen 3 2200G

RAM: G SKILL Ripjaws 8GB 2400Mhz

MoBo: GigaByte A320M - HD2

Storage: WD Green SSD

Case: Cooler Master 

PSU: Cooler Master Elite 400W

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Holy Grail said:

i mean not really, as long as you have 4 it wont matter

So, I suppose it doesn't matter...?

PC

 

CPU : Ryzen 3 2200G

RAM: G SKILL Ripjaws 8GB 2400Mhz

MoBo: GigaByte A320M - HD2

Storage: WD Green SSD

Case: Cooler Master 

PSU: Cooler Master Elite 400W

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, UnspeC5d said:

I started out Python a few months ago, I really liked the language, but my PC wasn't capable handling the interpreter (it's very old). Thus, I wanted to buy a new PC and this question arose in my mind, as I don't have the budget to buy a PC with i5 8th Gen which has 6 cores, and I rather thought of settling for the i3 8th Gen.

So does it make any difference?

Well, to directly answer your question, with much difficulty and time investment you can kind of get python to actually execute multiple threads simultaneously. But the language is not built for that, and is in fact, built to prevent that. (Lookup Global Interpreter Lock for more information).

But you won't stay with python forever. You will eventually branch out into other languages and technologies. My recommendation is to buy the best performing computer for all your needs. Don't go buying a very nice processor with little memory, don't go buying a crappy processor with lots of memory, and don't forget about the need for a GPU (since you seem to be interested in parallel programming, a logical goal would be to learn GPGPU stuff).

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, straight_stewie said:

Well, to directly answer your question, with much difficulty and time investment you can kind of get python to actually execute multiple threads simultaneously. But the language is not built for that, and is in fact, built to prevent that. (Lookup Global Interpreter Lock for more information).

But you won't stay with python forever. You will eventually branch out into other languages and technologies. My recommendation is to buy the best performing computer for all your needs. Don't go buying a very nice processor with little memory, don't go buying a crappy processor with lots of memory, and don't forget about the need for a GPU (since you seem to be interested in parallel programming, a logical goal would be to learn GPGPU stuff).

Yep, I would surely think about learning other languages, so I suppose a quad core with 8 gigs of RAM would be enough to start from?

Thanks for your answer

PC

 

CPU : Ryzen 3 2200G

RAM: G SKILL Ripjaws 8GB 2400Mhz

MoBo: GigaByte A320M - HD2

Storage: WD Green SSD

Case: Cooler Master 

PSU: Cooler Master Elite 400W

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, straight_stewie said:

Well, to directly answer your question, with much difficulty and time investment you can kind of get python to actually execute multiple threads simultaneously. But the language is not built for that, and is in fact, built to prevent that. (Lookup Global Interpreter Lock for more information).

Threads are not the same as running on multiple cores. Your are confusing multi threading and multi processing. Both of which are relatively easy to do in python.

 

Multiprocessing in python (running on multiple cores) is as simple as having a list of work and using the multiprocessing module to make a pool of cores and then map the list to a function to process.


from multiprocessing import Pool

 

def f(x):

    return x*x

 

mylist = [1,2,3,4,5]

pool = Pool(2)

pool.map(f, myList)

 

This will square the list across 2 cpu cores. If you need to access the result you can do a for on the map

 

For result in pool.map():

    print(result)

 

There is a downside and that's each core gets a copy of the list this with large lists can be very memory intensive.

 

You can somewhat get around this by using generators but that's another topic for another day.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, vorticalbox said:

Threads are not the same as running on multiple cores. Your are confusing multi threading and multi processing. Both of which are relatively easy to do in python.

 

Multiprocessing in python (running on multiple cores) is as simple as having a list of work and using the multiprocessing module to make a pool of cores and then map the list to a function to process.

 


from multiprocessing import Pool

 

def f(x):

    return x*x

 

mylist = [1,2,3,4,5]

pool = Pool(2)

pool.map(f, myList)

 

 

This will square the list across 2 cpu cores. If you need to access the result you can do a for on the map

 

For result in pool.map():

    print(result)

 

There is a downside and that's each core gets a copy of the list this with large lists can be very memory intensive.

 

You can somewhat get around this by using generators but that's another topic for another day.

Thanks for that code! 

PC

 

CPU : Ryzen 3 2200G

RAM: G SKILL Ripjaws 8GB 2400Mhz

MoBo: GigaByte A320M - HD2

Storage: WD Green SSD

Case: Cooler Master 

PSU: Cooler Master Elite 400W

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, UnspeC5d said:

Thanks for that code! 

Welcome on some forum somewhere I wrote a large post about multi processing/treads/async but i don't remember lol I'll try find it.

 

Depending on how much you want to learn I would look into gevent (a threading module), multiprocessing, generators too.

 

Remember it doesn't have to be a list it just has to be an interable.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Keep in mind that some other Python implementations don't have GIL (CPython is the official Python implementation in C).

Link to comment
Share on other sites

Link to post
Share on other sites

Entirely dependent on the programmer that's using it.

 

Edit: Actually read you're post and you're asking if the interpreter is multithreaded... which I'm a bit confused by since you could just compile your python code so it's no longer being interpreted.

Link to comment
Share on other sites

Link to post
Share on other sites

The reference Python interpreter, CPython, has something called the Global Interpreter Lock, or GIL. The problem with the GIL is that it forces only one thread to execute at a time. So you can have multiple threads in the ready state, but only one gets to run until it completes or gets blocked. You can get around this by starting multiple interpreter processes using the multiprocess module. 

 

This leads to an interesting way of doing "multithreading" in Python if you're using CPython as the interpreter. If your application is doing a CPU bound task, you have to use the multiprocess module to use multiple cores. If your application has a lot of I/O waits, then using multiple threads is better.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×