Code Monkey home page Code Monkey logo

snowflake's Introduction

Snowflake

Python implementation of Twitter's Snowflake.

NOTE : This is not a 1:1 implementation, it has been altered to fit my needs.

Explainations

Each time you create a Snowflake() instance, you increment its counter by 1 which decrease the chance of collisions if you just call it a bunch in your program

the maximum you can reach is 63 instances at once.


you can also use the process_id argument when (or if) you are using multiprocessing this one is manual you need to specifie it Snowflake(process_id=1) for each process

the maximum value is also 63.


Currently you should be able to generate:

  • 1023 ids per instance per process on the same millisecond.

so ... (1023 * 1000) = 1 023 000 ~ id/s

however the stress_test.py gives me:

  • 1M ids in less than a second on multiprocessing ( i5-9400 6 cores )
  • 500K ids in approx a second on juste one instance

they are things that need improvements, but for my use case it does work perfectly.

Current Structure

currently using 64 bits to store:

  • timestamp -> 42 bits -> maximum : 4398046511103
  • process_id -> 6 bits -> maximum : 63
  • instance_id -> 6 bits -> maximum : 63
  • sequences -> 10 bits -> maximum : 1023

Usage

Generating ids

let say the current timestamp is 1631090820684

from snowflake import Snowflake

# will generate a snowflake -> 1414366371759784960
snowflake = Snowflake()

snowflake.timestamp     # -> 1631090820.684
snowflake.to_date       # -> 08-09-2021 | 10:47:00
snowflake.to_binary     # -> 1001110100000110101101011100010110011000000000000010000000000
snowflake.to_hex        # -> 13a0d6b8b3000400

You can also use an existing snowflake

from snowflake import Snowflake

snowflake = Snowflake(1414424474358383616)

snowflake.timestamp     # -> 1631104673.423
snowflake.to_date       # -> 08-09-2021 | 14:37:53
snowflake.to_binary     # -> 1001110100001000010111001000011000011110000000000010000000000
snowflake.to_hex        # -> 13a10b90c3c00400

Iterating

from snowflake import Snowflake

for snowflake in Snowflake():
    snowflake  # int -> 1414425848173299712, 1414425848173299713, 1414425848173299714, ...

Optimizations

There is currently no checking if the time went backward ( timezones ) this will surely be a problem if you are generating a lot of ids per day

also, im not a pro python (even tho im currently working) doesn't mean its perfect, things are maybe done the wrong way..

don't hesitate to do a pull request if you found something wrong !

Installation

pip install git+https://github.com/Madscientiste/Snowflake.git#egg=Snowflake

[or, get the main experiment branch]
pip install git+https://github.com/Madscientiste/Snowflake.git@experimental#egg=Snowflake

snowflake's People

Contributors

madscientiste avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.