Python,重新进入`with`块

时间:2016-04-26 03:55:02

标签: python

我目前正在开发一个导入脚本,该脚本从数据库导入列表,该数据库每15分钟定期关闭以重新捕捉。

我创建了一个with块,如下所示,在创建连接时会考虑重试机制:

class DBRetryController(object):
    conn_obj = None
    connection = None
    cursor = None
    retry_count_down = None
    sleep_time = None

    def __init__(self, conn_obj, retry_count_down=5, sleep_time=10):
        self.conn_obj = conn_obj
        self.retry_count_down = retry_count_down
        self.sleep_time = sleep_time

    def __enter__(self):
        ex = None
        while self.retry_count_down > 0:
            try:
                if hasattr(self.conn_obj, '__call__'):
                    self.connection = self.conn_obj()
                else:
                    self.connection = self.conn_obj
                self.cursor = self.connection.cursor()
                self.retry_count_down = False
            except OperationalError as ex:
                log.warning('Caught db error, possibly due to sql server gone away, retrying in a few moment')
                self.retry_count_down -= 1
                time.sleep(self.sleep_time)
        if ex:
            raise ex
        return self.connection, self.cursor

    def __exit__(self, type, value, traceback):
        try:
            self.cursor.close()
            self.connection.close()
        except:
            pass

        if value:
            raise value

使用如下:

with DBRetryController(self.connection) as (_, cursor):
    cursor.execute(self.LISTING_QUERY)

但问题是服务器在执行查询时可能会关闭,是否可以修改DBRetryController以使嵌套的代码块重新进入?

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,我认为你可以使用这样的方案:

notCompleted = 1
class TestClass():
    def run(self):
        global notCompleted
        notCompleted = 1
        #do_something here
        notCompleted = 0


test =  TestClass()
test.run()
while(notCompleted):
    test.run()

假设我想确定,即使在执行run()方法期间发生任何错误,我的程序也会重试完成再次运行它。默认情况下,notCompleted1。当我在开始时调用run方法时,我会为其1分配run,并在0方法的末尾为其分配run()Try...Catch内的任何地方如果我有任何问题,在while循环中该函数将再次调用。

我认为您还需要添加public static ArrayList<Integer> mergeSort(ArrayList<Integer> merge) { if (merge.size() == 1) { return merge; } else { int halfway = merge.size() / 2; ArrayList<Integer> left = new ArrayList<Integer>(halfway); ArrayList<Integer> right = new ArrayList<Integer>(merge.size() - halfway); for (int i = 0; i < halfway; i++) { left.add(merge.get(i)); } for (int i = halfway; i < merge.size(); i++) { right.add(merge.get(i)); } left = mergeSort(left); right = mergeSort(right); int index1 = 0; int index2 = 0; // Merge left and right sub-lists into original list // See how the newMerge list is no longer needed for (int i = 0; i < merge.size(); i++) { if (index1 == left.size()) { merge.set(i, right.get(index2)); index2++; } else if (index2 == right.size()) { merge.set(i, left.get(index1)); index1++; } else { if (left.get(index1) <= right.get(index2)) { merge.set(i, left.get(index1)); // We now set the values into merge index1++; } else if (left.get(index1) >= right.get(index2)) { merge.set(i, right.get(index2)); // We now set the values into merge index2++; } } } return merge; // We now return a reference to merge, not newMerge } } public static boolean isOrdered(ArrayList<Integer> order) { for (int i = 0; i < order.size() - 1; i++) { // order.size() - 1 prevents going out of bounds if (order.get(i) > order.get(i + 1)) { return false; } } return true; }