From the course: Faster Python Code

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Threads

Threads - Python Tutorial

From the course: Faster Python Code

Start my 1-month free trial

Threads

- [Instructor] Since threads share the same memory, we need to be careful when accessing data structures from several threads. Python's data structures are not thread safe. You might think that's an odd design decision, however, most Python programs, even these days, run in a single thread. And there's a big performance penalty in making every data structure in Python thread safe. Getting rocks right is very tricky. Newer Python versions comes with concurrent.futures which simplifies working with threads. Say we'd like to get some information about GitHub users. We have their log in name and would like to know their real name and when they joined GitHub. GitHub provides a rest API we can query. Looking at our code in thr_pool.py, we have, in line 11, a function that gets the user info from the GitHub API. And, in line 20, we have function that gets the user information for several people in one go. In line 25, we define…

Contents