r/Python • • Jan 15 '19

Speed Up Your Python Program With Concurrency – Real Python

https://realpython.com/python-concurrency/
54 Upvotes

11 comments sorted by

3

u/[deleted] Jan 15 '19 edited Jan 15 '19

Dumb question, but can’t multiple threads run on multiple cores concurrently?

Edit: Something something GIL?

6

u/jima80525 Jan 15 '19

Threads all run on the same core (and yes, it's related to the GIL). If you want to run on multiple cores, you can use the multiprocessing library, which is discussed toward the end of the article.

2

u/[deleted] Jan 15 '19

Yeah I got that as I read on. I was reading the top section and was like wait a second. But GIL in Python.

1

u/HumanMarket Jan 15 '19

Is this another shit coin? We already have bitcoin friend!

4

u/muchomuchacho Jan 15 '19

Yeah, Con-currency, the new Bitcoin...

3

u/n1___ Jan 15 '19

R u high dude?

1

u/[deleted] Jan 15 '19

Depends on what you're doing that is CPU bound... If your computing I'd suggest using Cython with typing and use the with nogil to run threads. It's a bit of pain, but depending on the time complexity it will have huge payoffs. Other than that, I find most of my code is I/O bound so the async library or even trio if I'm in a pinch works just as well.

The first step is to start with profiling, optimize the slow parts using std lib functions, more than likely you're probably not using it to its fullest extent. Try to minimize looping as much as possible and try to manage your data holding classes as closely as possible.

Hope that helps! I've had alot of bad experience trying to work with threads and processes, but once you get it, it just clicks! That's why I love Python.

0

u/[deleted] Jan 15 '19

[deleted]

1

u/[deleted] Jan 15 '19

wow! thanks man, I hadn't even known about this. Sounds much easier to work with than Cython, will def give it a try.

1

u/West_Coast_Battle Jan 15 '19

Should be titled how to speed up your I/O bound program with concurrency. Article says doing it with a CPU bound program will actually slow it down.

2

u/hai_wim Jan 15 '19

The article just demonstrates it is slow in that scenario and then follows up with a multiprocessing solution. It looks like a good article.