Python 3.x python应用程序中可能有多个实例的全局变量

Python 3.x python应用程序中可能有多个实例的全局变量,python-3.x,recursion,Python 3.x,Recursion,我正在编写一个Python/MongoDB/AWS Lambda应用程序来将约会信息存储在数据库中,并可以选择对另一个约会进行“优先级排序”(通过使用一个名为“priority”的键值) 作为实现的一部分,我有一个名为get_next的函数,它从DB中提取下一个打开的约会并返回它(如果没有优先约会),或者返回优先约会(如果存在) def get_next(self, appointmentid= None): keydict = { 'sessionid' : self.

我正在编写一个Python/MongoDB/AWS Lambda应用程序来将约会信息存储在数据库中,并可以选择对另一个约会进行“优先级排序”(通过使用一个名为“priority”的键值)

作为实现的一部分,我有一个名为get_next的函数,它从DB中提取下一个打开的约会并返回它(如果没有优先约会),或者返回优先约会(如果存在)

def get_next(self, appointmentid= None):
    keydict = {
        'sessionid' : self.info['id'],
        'status' : 'open'
    }
    if appointmentid not in (None, ''):
        keydict.update({'appointmentid' : appointmentid })

    next_in_queue = self.db.getdetailsbykey("appointments", keydict, sortfield='_id', asc=False, suppressid=True)

    if 'priority' in next_in_queue and next_in_queue['priority'] not in ('', None):  
        priority = self.get_next(appointmentid=next_in_queue['priority'])
        if priority is not None:
            return priority
    return next_in_queue
这里getdetailsbykey是PyMongo的find_one的包装:

def getdetailsbykey(self, collection_name, keydict, sortfield=None, asc=False, suppressid = True):
        #if 'dict' not in str(type(keydict)):
        if type(keydict) is not dict:
            return None
        #collection = self.db[collection_name]
        collection = self.db[collection_name].with_options(codec_options=CodecOptions(tz_aware=True, tzinfo=timezone))
        if sortfield is None:
            if suppressid:
                data = collection.find_one(keydict,  {'_id' : 0})
            else:
                data = collection.find_one(keydict)
        else:
            if asc is True:
                sortoption = 1
            else:
                sortoption = -1
            if suppressid:
                data = collection.find_one(keydict, {'_id' : 0}, sort=[(sortfield, sortoption)], )
            else:
                data = collection.find_one(keydict, sort=[(sortfield, sortoption)])
        return data
然而,很明显,如果存在优先顺序的循环安排,这很容易受到“循环”问题的影响。通常,我会解决这个问题,但在这种情况下,我不确定这是否是一个在AWS lambda实例上运行的应用程序,所以我的问题是-实例是否会共享变量(以及值-这将是一件坏事(tm))

我想用递归来代替,但我觉得与循环相比,递归是更好的处理方法

编辑/更新: 我找到的最接近解决方案如下:

def get_next(self, appointmentid= None, ref_appt = None):
        keydict = {
            'sessionid' : self.info['id'],
            '$or' : [{ 'status' : 'open'}, { 'status' : 'inprogress'}]
        }
        if appointmentid not in (None, ''):  # if we have a specific id
            keydict.pop('$or', None)
            keydict.update({'id' : appointmentid })  # retrieve it

        next_in_queue = self.db.getdetailsbykey("appointments", keydict, sortfield='_id', asc=False, suppressid=True)
        if ref_appt is None:  # if this is the 0th level of recursion
            ref_appt = next_in_queue['id']  #save the id

        if next_in_queue is not None and 'priority' in next_in_queue and next_in_queue['priority'] not in ('', None):  # if there is a higher priority appointment
            priority = self.get_next(appointmentid=next_in_queue['priority'], ref_appt= ref_appt])
            if priority is not None:  # retrieve the appointment 
                if ref_appt != priority['id']:
                    return priority  # if there is a circular loop, this will not execute

        return next_in_queue
如果存在循环,这将导致返回循环中的最后一个元素。然而,我的核心问题仍然存在——在可能有多个实例的python应用程序中,全局变量是否在多个实例之间共享


欢迎任何指示/输入。

谢谢您的拼写检查,@marc\s。我不是以英语为母语的人,所以有时候我会把“ie”和“ei”混在一起,比如retrieve和retrieve:)谢谢你的拼写检查,@marc_.s。不是以英语为母语的人,所以有时我会混淆“ie”和“ei”,例如retrieve和retrieve:)