Snowflake ID Generator

raw JSON →
1.0.2 verified Mon Apr 27 auth: no python

A Python library for generating Snowflake-like unique IDs, inspired by Twitter's Snowflake ID. Version 1.0.2 supports Python 3.8+ and provides a simple interface for distributed unique ID generation with configurable epoch, worker ID, and sequence. The library is stable but not frequently updated.

pip install snowflake-id
error ImportError: cannot import name 'SnowflakeGenerator' from 'snowflake_id'
cause Incorrect import path. The package name on PyPI is 'snowflake-id', but the import module is 'snowflake'.
fix
Run: pip install snowflake-id, then import: from snowflake import SnowflakeGenerator
error AttributeError: 'SnowflakeGenerator' object has no attribute 'generate'
cause Trying to call a method like gen.generate() instead of using the iterator protocol.
fix
Use next(gen) to get the next ID.
gotcha The SnowflakeGenerator is an iterator; you must call next() or iterate over it. Many users mistakenly call gen.generate() or gen.get_id() which don't exist.
fix Use next(gen) or for id in gen: ...
gotcha The worker ID range is 0-1023 (10 bits). Using a value outside this range may cause unexpected behavior or errors. The library does not validate strictly.
fix Ensure worker ID is an integer between 0 and 1023.
gotcha Snowflake IDs are not guaranteed to be strictly increasing across multiple processes with different worker IDs if system clocks are not synchronized. This is by design but can cause confusion.
fix Use NTP or similar time synchronization across workers.

Create a generator with a worker ID (0-1023) and generate IDs by calling next().

from snowflake import SnowflakeGenerator

gen = SnowflakeGenerator(42)  # instance (worker ID)
snowflake_id = next(gen)
print(snowflake_id)