The Python Oracle

Thread safety in Python (Question how it works)

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Peaceful Mind

--

Chapters
00:00 Thread Safety In Python (Question How It Works)
00:56 Accepted Answer Score 1
01:26 Thank you

--

Full question
https://stackoverflow.com/questions/3164...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #multithreading

#avk47



ACCEPTED ANSWER

Score 1


Locals are thread-safe as they are not shared between threads. All constants (variables you never write to from any thread) are thread-safe. If that's all you have, then yes, that's fine. Ensure the class members you are talking about are really not written from any other thread.

Check there are no underlying shared resources that might not be thread-safe, eg. if each thread is using the database connection object that may cause trouble, unless that object is specifically documented as being thread-safe.