René's URL Explorer Experiment


Title: Python Thread Safety: Using a Lock and Other Techniques – Real Python

Open Graph Title: Python Thread Safety: Using a Lock and Other Techniques – Real Python

Description: In this tutorial, you'll learn about the issues that can occur when your code is run in a multithreaded environment. Then you'll explore the various synchronization primitives available in Python's threading module, such as locks, which help you make your code safe.

Open Graph Description: In this tutorial, you'll learn about the issues that can occur when your code is run in a multithreaded environment. Then you'll explore the various synchronization primitives available in Python's threading module, such as locks, which help you make your code safe.

Mail addresses
?subject=Python article for you&body=Python Thread Safety: Using a Lock and Other Techniques on Real Python https://realpython.com/python-thread-lock/

Opengraph URL: https://realpython.com/python-thread-lock/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
{
  "@context": "http://schema.org",
  "@type": "Article",
  "headline": "Python Thread Safety: Using a Lock and Other Techniques",
  "image": {
    "@type": "ImageObject",
    "url": "https://files.realpython.com/media/Thread-Safety-in-Python_Watermarked.434d0dbc3127.jpg",
    "width": 1920,
    "height": 1080
  },
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "https://realpython.com/python-thread-lock/",
    "lastReviewed": "2024-08-01",
    "author": {
      "@type": "Person",
      "name": "Adarsh Divakaran",
      "image": "https://realpython.com/cdn-cgi/image/width=2048,height=2048,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Headshot_square.4385e61c51b4.JPG",
      "url": "https://realpython.com/team/adevamritham/",
      "affiliation": {
        "@type": "Organization",
        "@id": "https://realpython.com/#organization",
        "name": "Real Python",
        "url": "https://realpython.com",
        "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
      }
    },
    "reviewedBy": [
      {
        "@type": "Person",
        "name": "Aldren Santos",
        "image": "https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png",
        "url": "https://realpython.com/team/asantos/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Brenda Weleschuk",
        "image": "https://realpython.com/cdn-cgi/image/width=320,height=320,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/IMG_3324_1.50b309355fc1.jpg",
        "url": "https://realpython.com/team/bweleschuk/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Geir Arne Hjelle",
        "image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/gahjelle.470149ee709e.jpg",
        "url": "https://realpython.com/team/gahjelle/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Martin Breuss",
        "image": "https://realpython.com/cdn-cgi/image/width=456,height=456,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/martin_breuss_python_square.efb2b07faf9f.jpg",
        "url": "https://realpython.com/team/mbreuss/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      }
    ]
  },
  "datePublished": "2024-10-23T14:00:00+00:00",
  "dateModified": "2024-08-01T10:33:31.574010+00:00",
  "publisher": {
    "@type": "Organization",
    "@id": "https://realpython.com/#organization",
    "name": "Real Python",
    "url": "https://realpython.com",
    "logo": {
      "@type": "ImageObject",
      "url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
      "width": 512,
      "height": 512
    },
    "description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
    "slogan": "Become a Python Expert",
    "email": "info@realpython.com",
    "sameAs": [
      "https://github.com/realpython",
      "https://www.youtube.com/realpython",
      "https://twitter.com/realpython",
      "https://x.com/realpython",
      "https://www.linkedin.com/company/realpython-com/",
      "https://www.facebook.com/learnrealpython",
      "https://www.instagram.com/realpython",
      "https://www.tiktok.com/@realpython.com"
    ]
  },
  "author": {
    "@type": "Person",
    "name": "Adarsh Divakaran",
    "image": "https://realpython.com/cdn-cgi/image/width=2048,height=2048,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Headshot_square.4385e61c51b4.JPG",
    "url": "https://realpython.com/team/adevamritham/",
    "affiliation": {
      "@type": "Organization",
      "@id": "https://realpython.com/#organization",
      "name": "Real Python",
      "url": "https://realpython.com",
      "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
    }
  },
  "description": "In this tutorial, you'll learn about the issues that can occur when your code is run in a multithreaded environment. Then you'll explore the various synchronization primitives available in Python's threading module, such as locks, which help you make your code safe."
}

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/Thread-Safety-in-Python_Watermarked.434d0dbc3127.jpg
og:imagehttps://files.realpython.com/media/Thread-Safety-in-Python_Watermarked.434d0dbc3127.jpg
twitter:creator@realpython
og:typearticle

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/python-thread-lock/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/python-thread-lock/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Fpython-thread-lock%2F
Browse Topicshttps://realpython.com/tutorials/all/
Guided Learning Pathshttps://realpython.com/learning-paths/
Basicshttps://realpython.com/search?level=basics
Intermediatehttps://realpython.com/search?level=intermediate
Advancedhttps://realpython.com/search?level=advanced
aihttps://realpython.com/tutorials/ai/
algorithmshttps://realpython.com/tutorials/algorithms/
apihttps://realpython.com/tutorials/api/
best-practiceshttps://realpython.com/tutorials/best-practices/
careerhttps://realpython.com/tutorials/career/
communityhttps://realpython.com/tutorials/community/
databaseshttps://realpython.com/tutorials/databases/
data-sciencehttps://realpython.com/tutorials/data-science/
data-structureshttps://realpython.com/tutorials/data-structures/
data-vizhttps://realpython.com/tutorials/data-viz/
devopshttps://realpython.com/tutorials/devops/
djangohttps://realpython.com/tutorials/django/
dockerhttps://realpython.com/tutorials/docker/
editorshttps://realpython.com/tutorials/editors/
flaskhttps://realpython.com/tutorials/flask/
front-endhttps://realpython.com/tutorials/front-end/
gamedevhttps://realpython.com/tutorials/gamedev/
guihttps://realpython.com/tutorials/gui/
machine-learninghttps://realpython.com/tutorials/machine-learning/
newshttps://realpython.com/tutorials/news/
numpyhttps://realpython.com/tutorials/numpy/
projectshttps://realpython.com/tutorials/projects/
pythonhttps://realpython.com/tutorials/python/
stdlibhttps://realpython.com/tutorials/stdlib/
testinghttps://realpython.com/tutorials/testing/
toolshttps://realpython.com/tutorials/tools/
web-devhttps://realpython.com/tutorials/web-dev/
web-scrapinghttps://realpython.com/tutorials/web-scraping/
Table of Contentshttps://realpython.com/python-thread-lock/#toc
Threading in Pythonhttps://realpython.com/python-thread-lock/#threading-in-python
Understanding Thread Safetyhttps://realpython.com/python-thread-lock/#understanding-thread-safety
The GIL and Its Implications on Threadinghttps://realpython.com/python-thread-lock/#the-gil-and-its-implications-on-threading
Race Conditionshttps://realpython.com/python-thread-lock/#race-conditions
Synchronization Primitiveshttps://realpython.com/python-thread-lock/#synchronization-primitives
Using Python Threading Locks for Mutual Exclusionhttps://realpython.com/python-thread-lock/#using-python-threading-locks-for-mutual-exclusion
threading.Lock for Primitive Lockinghttps://realpython.com/python-thread-lock/#threadinglock-for-primitive-locking
threading.RLock for Reentrant Lockinghttps://realpython.com/python-thread-lock/#threadingrlock-for-reentrant-locking
Limiting Access With Semaphoreshttps://realpython.com/python-thread-lock/#limiting-access-with-semaphores
Using Synchronization Primitives for Communication and Coordinationhttps://realpython.com/python-thread-lock/#using-synchronization-primitives-for-communication-and-coordination
Events for Signalinghttps://realpython.com/python-thread-lock/#events-for-signaling
Conditions for Conditional Waitinghttps://realpython.com/python-thread-lock/#conditions-for-conditional-waiting
Barriers for Coordinationhttps://realpython.com/python-thread-lock/#barriers-for-coordination
Deciding When to Use Synchronization Primitiveshttps://realpython.com/python-thread-lock/#deciding-when-to-use-synchronization-primitives
Conclusionhttps://realpython.com/python-thread-lock/#conclusion
https://realpython.com/feedback/survey/article/python-thread-lock/liked/?from=article-sidebar
https://realpython.com/feedback/survey/article/python-thread-lock/disliked/?from=article-sidebar
Thread Safety in Python: Locks and Other Techniqueshttps://realpython.com/courses/thread-safety-locks-other-techniques/
Adarsh Divakaranhttps://realpython.com/python-thread-lock/#author
https://realpython.com/python-thread-lock/#reader-comments
intermediatehttps://realpython.com/tutorials/intermediate/
pythonhttps://realpython.com/tutorials/python/
stdlibhttps://realpython.com/tutorials/stdlib/
Threading in Pythonhttps://realpython.com/python-thread-lock/#threading-in-python
Understanding Thread Safetyhttps://realpython.com/python-thread-lock/#understanding-thread-safety
The GIL and Its Implications on Threadinghttps://realpython.com/python-thread-lock/#the-gil-and-its-implications-on-threading
Race Conditionshttps://realpython.com/python-thread-lock/#race-conditions
Synchronization Primitiveshttps://realpython.com/python-thread-lock/#synchronization-primitives
Using Python Threading Locks for Mutual Exclusionhttps://realpython.com/python-thread-lock/#using-python-threading-locks-for-mutual-exclusion
threading.Lock for Primitive Lockinghttps://realpython.com/python-thread-lock/#threadinglock-for-primitive-locking
threading.RLock for Reentrant Lockinghttps://realpython.com/python-thread-lock/#threadingrlock-for-reentrant-locking
Limiting Access With Semaphoreshttps://realpython.com/python-thread-lock/#limiting-access-with-semaphores
Using Synchronization Primitives for Communication and Coordinationhttps://realpython.com/python-thread-lock/#using-synchronization-primitives-for-communication-and-coordination
Events for Signalinghttps://realpython.com/python-thread-lock/#events-for-signaling
Conditions for Conditional Waitinghttps://realpython.com/python-thread-lock/#conditions-for-conditional-waiting
Barriers for Coordinationhttps://realpython.com/python-thread-lock/#barriers-for-coordination
Deciding When to Use Synchronization Primitiveshttps://realpython.com/python-thread-lock/#deciding-when-to-use-synchronization-primitives
Conclusionhttps://realpython.com/python-thread-lock/#conclusion
Remove adshttps://realpython.com/account/join/
Thread Safety in Python: Locks and Other Techniqueshttps://realpython.com/courses/thread-safety-locks-other-techniques/
Click here to download the free sample codehttps://realpython.com/bonus/python-thread-lock-code/
https://realpython.com/quizzes/python-thread-lock/
Python Thread Safety: Using a Lock and Other Techniqueshttps://realpython.com/quizzes/python-thread-lock/
https://realpython.com/python-thread-lock/#threading-in-python
threadinghttps://realpython.com/intro-to-python-threading/#what-is-a-thread
concurrency frameworkhttps://realpython.com/python-concurrency/
context switchhttps://en.wikipedia.org/wiki/Context_switch
Sourcehttps://docs.python.org/3/library/sys.html#sys.setswitchinterval
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-thread-lock/#understanding-thread-safety
Thread safetyhttps://en.wikipedia.org/wiki/Thread_safety
https://realpython.com/python-thread-lock/#the-gil-and-its-implications-on-threading
Global Interpreter Lock (GIL)https://realpython.com/python-gil/
mutexhttps://en.wikipedia.org/wiki/Lock_(computer_science)
CPU-bound programshttps://realpython.com/python-concurrency/#how-to-speed-up-a-cpu-bound-program
parallelismhttps://realpython.com/python-concurrency/#what-is-parallelism
IO-bound taskshttps://realpython.com/python-concurrency/#how-to-speed-up-an-io-bound-program
atomic operationshttps://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe
The GIL can be releasedhttps://docs.python.org/3/glossary.html#term-global-interpreter-lock
race conditionhttps://realpython.com/python-thread-lock/#race-conditions
GIL-free Python interpreterhttps://realpython.com/python-news-october-2023/#pep-703-accepted-one-step-closer-to-python-without-the-gil
may be the defaulthttps://peps.python.org/pep-0703/#python-build-modes
https://realpython.com/python-thread-lock/#race-conditions
race conditionhttps://en.wikipedia.org/wiki/Race_condition
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-thread-lock/#synchronization-primitives
synchronization primitiveshttps://en.wikipedia.org/wiki/Synchronization_(computer_science)#Implementation
https://realpython.com/python-thread-lock/#using-python-threading-locks-for-mutual-exclusion
https://realpython.com/python-thread-lock/#threadinglock-for-primitive-locking
threading modulehttps://docs.python.org/3/library/threading.html
with statementhttps://realpython.com/python-with-statement/
queue.Queue workerhttps://realpython.com/intro-to-python-threading/#producer-consumer-using-queue
databasehttps://realpython.com/learning-paths/database-access-in-python/
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-thread-lock/#threadingrlock-for-reentrant-locking
deadlockhttps://en.wikipedia.org/wiki/Deadlock
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-thread-lock/#limiting-access-with-semaphores
semaphorehttps://realpython.com/intro-to-python-threading/#semaphore
https://realpython.com/python-thread-lock/#using-synchronization-primitives-for-communication-and-coordination
https://realpython.com/python-thread-lock/#events-for-signaling
Eventhttps://docs.python.org/3/library/threading.html#event-objects
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-thread-lock/#conditions-for-conditional-waiting
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-thread-lock/#barriers-for-coordination
Barrierhttps://docs.python.org/3/library/threading.html#threading.Barrier
https://realpython.com/python-thread-lock/#deciding-when-to-use-synchronization-primitives
thread-local storagehttps://blog.adarshd.dev/posts/thread-local-storage-in-python/
Sourcehttps://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe
Remove adshttps://realpython.com/account/join/
https://realpython.com/python-thread-lock/#conclusion
Click here to download the free sample codehttps://realpython.com/bonus/python-thread-lock-code/
https://realpython.com/quizzes/python-thread-lock/
Python Thread Safety: Using a Lock and Other Techniqueshttps://realpython.com/quizzes/python-thread-lock/
https://realpython.com/feedback/survey/article/python-thread-lock/liked/?from=article-footer
https://realpython.com/feedback/survey/article/python-thread-lock/disliked/?from=article-footer
Thread Safety in Python: Locks and Other Techniqueshttps://realpython.com/courses/thread-safety-locks-other-techniques/
https://realpython.com/team/adevamritham/
» More about Adarshhttps://realpython.com/team/adevamritham/
https://realpython.com/team/asantos/
Aldrenhttps://realpython.com/team/asantos/
https://realpython.com/team/bweleschuk/
Brendahttps://realpython.com/team/bweleschuk/
https://realpython.com/team/gahjelle/
Geir Arnehttps://realpython.com/team/gahjelle/
https://realpython.com/team/mbreuss/
Martinhttps://realpython.com/team/mbreuss/
Level Up Your Python Skills »https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=python-thread-lock
Level Up Your Python Skills »https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=python-thread-lock
https://realpython.com/feedback/survey/article/python-thread-lock/liked/?from=article-comments
https://realpython.com/feedback/survey/article/python-thread-lock/disliked/?from=article-comments
LinkedInhttps://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Frealpython.com%2Fpython-thread-lock%2F
Twitterhttps://twitter.com/intent/tweet/?text=Interesting%20Python%20article%20on%20%40realpython%3A%20Python%20Thread%20Safety%3A%20Using%20a%20Lock%20and%20Other%20Techniques&url=https%3A%2F%2Frealpython.com%2Fpython-thread-lock%2F
Blueskyhttps://bsky.app/intent/compose?text=Interesting%20Python%20article%20on%20%40realpython.com%3A%20Python%20Thread%20Safety%3A%20Using%20a%20Lock%20and%20Other%20Techniques%20https%3A%2F%2Frealpython.com%2Fpython-thread-lock%2F
Facebookhttps://facebook.com/sharer/sharer.php?u=https%3A%2F%2Frealpython.com%2Fpython-thread-lock%2F
Get tips for asking good questionshttps://realpython.com/python-beginner-tips/#tip-9-ask-good-questions
get answers to common questions in our support portalhttps://support.realpython.com
Real Python Community Chathttps://realpython.com/community/
“Office Hours” Live Q&A Sessionhttps://realpython.com/office-hours/
intermediatehttps://realpython.com/tutorials/intermediate/
pythonhttps://realpython.com/tutorials/python/
stdlibhttps://realpython.com/tutorials/stdlib/
Thread Safety in Python: Locks and Other Techniqueshttps://realpython.com/courses/thread-safety-locks-other-techniques/
Speed Up Your Python Program With Concurrencyhttps://realpython.com/python-concurrency/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-thread-lock
An Intro to Threading in Pythonhttps://realpython.com/intro-to-python-threading/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-thread-lock
Python's asyncio: A Hands-On Walkthroughhttps://realpython.com/async-io-python/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-thread-lock
What Is the Python Global Interpreter Lock (GIL)?https://realpython.com/python-gil/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-thread-lock
Python's with Statement: Manage External Resources Safelyhttps://realpython.com/python-with-statement/?utm_source=realpython&utm_medium=web&utm_campaign=related-post&utm_content=python-thread-lock
https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fpython-thread-lock%2F
Continue »https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fpython-thread-lock%2F
Sign-Inhttps://realpython.com/account/login/?next=/python-thread-lock/
Start Herehttps://realpython.com/start-here/
Learning Resourceshttps://realpython.com/search
Code Mentorhttps://realpython.com/mentor/
Python Referencehttps://realpython.com/ref/
Python Cheat Sheethttps://realpython.com/cheatsheets/python/
Support Centerhttps://support.realpython.com/
Learning Pathshttps://realpython.com/learning-paths/
Quizzes & Exerciseshttps://realpython.com/quizzes/
Browse Topicshttps://realpython.com/tutorials/all/
Live Courseshttps://realpython.com/live/
Bookshttps://realpython.com/books/
Podcasthttps://realpython.com/podcasts/rpp/
Newsletterhttps://realpython.com/newsletter/
Community Chathttps://realpython.com/community/
Office Hourshttps://realpython.com/office-hours/
Learner Storieshttps://realpython.com/learner-stories/
Plans & Pricinghttps://realpython.com/account/join/
Team Planshttps://realpython.com/account/join-team/
For Businesshttps://realpython.com/account/join-team/inquiry/
For Schoolshttps://realpython.com/account/join-team/education-inquiry/
Reviewshttps://realpython.com/learner-stories/
About Ushttps://realpython.com/about/
Teamhttps://realpython.com/team/
Mission & Valueshttps://realpython.com/mission/
Editorial Guidelineshttps://realpython.com/editorial-guidelines/
Sponsorshipshttps://realpython.com/sponsorships/
Careershttps://realpython.workable.com
Press Kithttps://realpython.com/media-kit/
Merchhttps://realpython.com/merch
https://www.youtube.com/realpython
https://x.com/realpython
https://www.linkedin.com/company/realpython-com
https://www.facebook.com/LearnRealPython
https://github.com/realpython/
Privacy Policyhttps://realpython.com/privacy-policy/
Terms of Usehttps://realpython.com/terms/
Securityhttps://realpython.com/security/
Contacthttps://realpython.com/contact/
https://realpython.com/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.