April 29, 2024

Pluggy! Implementing plugin system in python

Plugin architecture is quite handy when you are designing a developer-facing library. A lot of times I wanted to allow users to inject their code in some way to extend the core library functions and add domain-specific customizations. I found a great approach while going through tox source code and discovered Pluggy which builds on top of Python’s entry_point feature. I was amazed to know thatentry_point is not just for CLI, but a clean plugin system that comes with Python....

April 23, 2024

Condition Variables - A cool synchronization technique!

I was a bit taken aback to come to know about condition-variables this late in my journey. Condition variables allow you to wait and notify other threads when a certain event occurs. I understood it best through an example. Following is a code sample for Producer-Consumer problem using mutexes. producer-consumer-just-mutex.py from threading import Thread, Lock import time, random mutex = Lock() items = [] def timer(t): while t: time.sleep(1) print(f"Time: {t}") t -= 1 def producer(): while True: with mutex: print("Acquired lock in producer") items....

April 6, 2024

Creating files bigger than available disk space!

spoiler: sparse files/holes in a file I was recently going through the book The Linux Programming Interface and came across the concept of sparse files. Files are stored on multiple blocks on a disk and sparse files is a smart optimization that skips allocating disk block if there is no data to be stored. You can create holes in your files by moving the file pointer n bytes ahead of the actual size and then writing some random data to it....