可能具有多个实例的python应用程序中的全局变量

时间:2019-05-07 07:41:06

标签: python-3.x recursion

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

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

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

但是,很明显,如果有优先次序的循环安排,则很容易出现“循环”问题。通常,我会解决这个using a global variable的问题,但是在这种情况下,我不确定这将是在AWS lambda实例上运行的应用程序,所以我的问题是-实例将共享变量(因此值也一样) -这将是一件不好的事情(tm))??

我会use a loop to do this而不是递归,但是与循环相比,我认为递归是一种更好的处理方式。

编辑/更新: 我最接近解决方案的是:

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应用程序中的全局变量会在实例之间共享吗?

任何指针/输入都将非常受欢迎。

0 个答案:

没有答案