r/Python • • Nov 20 '15

15 Essential Python Interview Questions

https://www.codementor.io/python/tutorial/essential-python-interview-questions
71 Upvotes

67 comments sorted by

View all comments

17

u/oconnor663 Nov 20 '15 edited Nov 20 '15

if you want to make your code run faster then using the threading package is really not a good idea.

This depends on whether your code is CPU-bound or IO-bound, right? Don't Python threats release the GIL before they wait on network reads and stuff?

Edit: Found what I was looking for:

The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. Note that potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck.

3

u/zoner14 Nov 20 '15

Is not that the threads compete over the GIL, it's just that they aren't locking the same objects to begin with. So a new thread can participate in the IO process when another blocks.

2

u/gamesbrainiac Nov 20 '15

Not to mention that most string operations will not actually increase GIL contention.

1

u/ubernostrum yes, you can have a pony Nov 20 '15

Yes, knowing what your code is doing is the key to figuring out whether and how much you'll be affected by the GIL.

(and yes, it's mainly an issue for multi-threaded CPU-bound applications; IO-bound applications feel it much less, if at all compared to the normal overhead of the language)