<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/singleton-design-pattern-in-python---part-2-making-it-thread-safe-ccibmf9ny" -->

---
title: Singleton Design Pattern in python - Part 2. Making it...
description: Explore how to enhance a Singleton design pattern in Python by making it thread-safe using mutex locks and double-checked locking. Learn key techniques to...
canonical: https://daily.dev/posts/singleton-design-pattern-in-python---part-2-making-it-thread-safe-ccibmf9ny
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Singleton Design Pattern in python - Part 2. Making it thread safe | daily.dev
og:description: Explore how to enhance a Singleton design pattern in Python by making it thread-safe using mutex locks and double-checked locking. Learn key techniques to...
og:url: https://daily.dev/posts/singleton-design-pattern-in-python---part-2-making-it-thread-safe-ccibmf9ny
og:image: https://api.daily.dev/og/posts/CCiBmF9NY.png
og:image:alt: Singleton Design Pattern in python - Part 2. Making it thread safe
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Singleton Design Pattern in python - Part 2. Making it thread safe

**[Learn Python](https://daily.dev/sources/lpython)** · [@bhavukkalra](https://daily.dev/bhavukkalra) · 1 min read · 4 upvotes · 3 comments

## Summary

Explore how to enhance a Singleton design pattern in Python by making it thread-safe using mutex locks and double-checked locking. Learn key techniques to ensure your Singleton remains effective when accessed by multiple threads.

## Content

The continuation of the series on Singleton design pattern in python. This part 2 continues on where the part 1 left off i.e with a question and the question was as follows. 

We have written a pretty good code so far, but it doesn't works as expected i.e It's not singleton anymore when multiple threads try to access it. 

We discuss a mutex lock solution and also touch up on double locking patterns along the way. Therefore making it an amazing read for the users who wanna learn how to write thread safe code in python using the architecture described below in the diagram

![thrrad_lock_acquiring](https://media.daily.dev/image/upload/s--X1ALAlh5--/f_auto/v1745085360/ugc/content_18ed256d-feba-4ff0-b867-fe9c6d5360be)

## Community discussion

Top comments from developers on daily.dev.

**@bhavukkalra** · 1 upvotes

> Here is the link to the post - [Singleton design Pattern part 2]([url](https://dev.to/bhavukkalra/python-singleton-logger-part-2-thread-safety-race-conditions-lock-optimization-1l2o))

---

Tags: [#python](https://daily.dev/tags/python), [#design-patterns](https://daily.dev/tags/design-patterns)

[View this post on daily.dev](https://daily.dev/posts/singleton-design-pattern-in-python---part-2-making-it-thread-safe-ccibmf9ny)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/singleton-design-pattern-in-python---part-2-making-it-thread-safe-ccibmf9ny","headline":"Singleton Design Pattern in python - Part 2. Making it thread safe","text":"Explore how to enhance a Singleton design pattern in Python by making it thread-safe using mutex locks and double-checked locking. Learn key techniques to ensure your Singleton remains effective when accessed by multiple threads.","url":"https://daily.dev/posts/singleton-design-pattern-in-python---part-2-making-it-thread-safe-ccibmf9ny","datePublished":"2025-04-20T10:46:18.425Z","dateModified":"2025-04-20T10:46:28.799Z","author":{"@type":"Person","name":"Bhavuk Kalra","url":"https://daily.dev/bhavukkalra","image":"https://lh3.googleusercontent.com/a/AAcHTtdht-qoOWkv4q1p8zJJfCxBSiz1ZItbwGWJmpuce6dLwyQ=s96-c","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":70}},"image":"https://media.daily.dev/image/upload/s--LRqO00J0--/f_auto/v1745085681/posts/vjqF4shVF","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":4},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":3}],"comment":[{"@type":"Comment","text":"Here is the link to the post - Singleton design Pattern part 2","datePublished":"2025-04-20T12:43:10.541Z","url":"https://daily.dev/posts/CCiBmF9NY#c-5ielIhTtd","author":{"@type":"Person","name":"Bhavuk Kalra","url":"https://daily.dev/bhavukkalra","image":"https://lh3.googleusercontent.com/a/AAcHTtdht-qoOWkv4q1p8zJJfCxBSiz1ZItbwGWJmpuce6dLwyQ=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/lpython","name":"Learn Python"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Learn Python","item":"https://daily.dev/squads/lpython"},{"@type":"ListItem","position":3,"name":"Singleton Design Pattern in python - Part 2. Making it thread safe"}]}
```

