Skip to content

Instantly share code, notes, and snippets.

View WazedKhan's full-sized avatar
:electron:
Praxis

Abdul Wajed Khan WazedKhan

:electron:
Praxis
View GitHub Profile
@WazedKhan
WazedKhan / az_vpn_ubuntu_24.md
Created March 17, 2025 03:57
Install Azure VPN Client on Ubuntu 24.04

Install Azure VPN Client on Ubuntu 24.04

Step 1: Install curl

sudo apt-get install -y curl

Step 2: Add Microsoft’s Public Key

curl -sSl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

When we say "it’s difficult to write thread-safe code", we're referring to the challenges of ensuring that multiple threads can execute simultaneously without causing unexpected behavior or data corruption. Let’s break it down:

What Does Thread Safety Mean?

In multi-threaded applications, different threads often share the same data or resources (such as variables, memory, or files). Thread-safe code ensures that:

  1. Shared resources are accessed in a way that prevents race conditions (where two threads try to modify the same data at the same time).
  2. No data corruption or inconsistencies arise from concurrent access to shared data.
  3. Threads do not experience deadlocks (where two threads wait on each other, causing the program to freeze).

Why Is Thread-Safe Code Difficult to Write?

Here are the key challenges that make writing thread-safe code difficult:

The new method in Python is a fundamental method that is responsible for creating instances of a class. It is often referred to as a "static" method, although technically it's a class method because Python implicitly passes the class (cls) as the first argument.

Purpose of __ new __

The primary role of __ new __ is to create and return a new instance of a class. It is called before __ init __, which initializes the created instance. Here’s a step-by-step breakdown:

  1. Class Creation: When we write a class and create an instance of it, Python internally calls __ new __ to create the object.

  2. Instance Initialization: After __ new __ creates the instance, Python then calls __ init __ to initialize it.

Basic Usage

def convert_image(input_image_path: str, output_image_path: str, output_format: str) -> None:
    try:
        # Open the input image
        with Image.open(input_image_path) as img:
            # Convert and save the image in the new format
            img.save(output_image_path, output_format)
        print(f"Image successfully converted to {output_format} and saved to {output_image_path}.")
    except Exception as e:
 print(f"An error occurred: {e}")
@WazedKhan
WazedKhan / signals.py
Last active November 6, 2023 14:48
Passing Arguments for Pre-Save and Post-Save Events
# Pass extra argument from pre save to post save
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
@receiver(pre_save, sender=YourModel)
def pre_save_handler(sender, instance, **kwargs):
# You can access the instance being saved and other arguments
your_argument = instance.some_value