unique number generator

Skip Montanaro skip at pobox.com
Wed May 19 09:30:50 EDT 2004


    I need to implement a unique number generator that 1 or more processes
    on same or different machines will make use of it. Is there any library
    / project available already for this?

If blazing performance isn't an issue I'd just use the SimpleXMLRPCServer
class to write a little server that increments a stored counter.  Something
like this:

    from SimpleXMLRPCServer import SimpleXMLRPCServer

    class Counter:
        def __init__(self):
            self.i = 0

        def tick(self):
            self.i += 1
            return self.i

    server = SimpleXMLRPCServer(('localhost', 10000))
    server.register_instance(Counter())
    server.serve_forever()

A client would then do something like

    import xmlrpclib
    counter = xmlrpclib.ServerProxy("http://localhost:10000")
    i = counter.tick()
    j = counter.tick()
    print i, j

Obviously you can get more elaborate than that (saving/restoring counters
across runs or allocating blocks of numbers for more efficiency, for
example), but the above appears to satisfy your initial requirements.

Skip




More information about the Python-list mailing list