Giving unique IDs to all nodes?

拟墨画扇 提交于 2019-12-02 05:49:24

You could keep a class variable and use it for ordinal ids:

class Node(object):
    _id = 0

    def __init__(self):
        self._id = Node._id
        Node._id += 1

It also has the benefit that your class will be able to know how many objects were altogether created.

This is also way cheaper than random ids.

If you just need a unique identifier, the built-in Python id() function would do it:

Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

Pretty much both of your solutions are what is done in practice.

Your first solution is to just increment a number will give you uniqueness, as long as you don't overflow (with python bigintegers this isnt really a problem). The disadvantage of this approach is if you start doing concurrency you have to make sure you use locking to prevent data races when increment and reading your external value.

The other approach where you generate a random number works well in the concurrency situation. The larger number of bits you use, the less likely it is you will run into a collision. In fact you can pretty much guarantee that you won't have collisions if you use say 128-bits for your id.

An approach you can use to further guarantee you don't have collisions, is to make your unique ids something like TIMESTAMP_HASHEDMACHINENAME_PROCESSID/THREADID_UNIQUEID. Then pretty much can't have collisions unless you generate two of the same UNIQUEID on the same process/thread within 1 second. MongoDB does something like this where they just increment the UNIQUEID. I am not sure what they do in the case of an overflow (which I assume doesn't happen too often in practice). One solution might be just to wait till the next second before generating more ids.

This is probably overkill for what you are trying to do, but it is a somewhat interesting problem indeed.

UUID is good for this sort of thing.

>>> from uuid import uuid4
>>> uuid4().hex
'461dd72c63db4ae9a969978daadc59f0'

Universally Unique ID's have very low collision rate -- unless you are creating billions of nodes, it should do the trick.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!