In Python App Engine How Do I Uniquely Identify An Instance Of My App Running On The Dev SDK?
Question: My application relies on an external service that it communicates with using
urlfetch. I have multiple developers each running their own instance of my
application on their development computers while they add features. Each
developer instance needs to be able to uniquely identify itself to the
external service so that the external service can keep their data separated.

I need a way to automatically generate a unique identifier for each developer
from within the application.

Yes, I could just have each developer put a unique id in a variable in their
code but I would much prefer it was automatic.

Also, I could probably read some information about the hardware on the
computer (like MAC address) and use that but I want this code to use only
things that work on the production server so that I can use it there
eventually as well.


Answer: The only trick I've seen to identify instances is using a global variable
address.

    
    
    UNIQUE_INSTANCE_ID = {} # at module level
    logging.debug("Instance %s." % (str("%X" % id( UNIQUE_INSTANCE_ID )).zfill(16)))
    

That seems to work fairly well to uniquely identify an instance; but it only
identifies an instance, not a machine. So if you restart your instance, you
get a new identifier. That might be a "feature".

You could also use some of the META variables; if developers are all running
out of a home directory, you could parse a username out of 'PATH_TRANSLATED'.

At the very least, you could make injecting a UUID into the datastore part of
the data population; store a metadata kind in the datastore and the cache, and
wrap that UUID into the requests.

    
    
    from uuid import uuid4
    from google.appengine.ext import db
    from google.appengine.api import memcache
    
    cache = memcache.Client()
    
    class InstanceStamp(db.Model):
        code = db.StringProperty()
    
        INSTANCE_STAMP_KEY = "instance_stamp"
    
        @classmethod
        def get_stamp(cls):
            cache_key = cls.INSTANCE_STAMP_KEY 
            stamp_code = cache.get(cache_key)
            if stamp_code is None:
                code = uuid4().hex
                stamp = cls.get_or_insert('instance_stamp', code=code)
                if stamp is not None:
                    cache.set(cache_key, stamp.code, 300)
                    stamp_code = stamp.code
            return stamp_code
    

