【问题标题】:APScheduler Threading design patternAPScheduler 线程设计模式
【发布时间】:2020-12-07 16:44:40
【问题描述】:

我目前正在编写一个应用程序来使用 APScheduler (3.6.0) 和 python3.6 安排一些指标

运行 5-6 天后,调度程序停止,没有任何错误。所以我认为这可能是资源/并发问题。

我有不同的时间表,例如每 15 分钟、每 30 分钟、每小时等...

我的调度程序初始化如下,我现在只使用线程:

    executors = {
    'default': ThreadPoolExecutor(60),
    'processpool': ProcessPoolExecutor(5)
}

scheduler = BackgroundScheduler(timezone="Europe/Paris", executors=executors)

我的问题如下:当多个作业开始时,通过相同的代码部分,这可能是一个潜在的概念错误吗?那我需要放一些锁吗? 例如,他们使用相同的代码部分连接到数据库(相同的模块)并检索结果

def executedb(data):
    cursor = None
    con = None

    try:
        dsn = cx.makedsn(data[FORMAT_CFG_DB_HOST], data[FORMAT_CFG_DB_PORT],
                         service_name=data[FORMAT_CFG_DB_SERVICE_NAME])
        con = cx.connect(user=data[FORMAT_CFG_DB_USER], password=data[FORMAT_CFG_DB_PASSWORD], dsn=dsn)

        cursor = con.cursor()
        sql = data[FORMAT_METRIC_SQL]

        cursor = cursor.execute(sql)

        rows = rows_to_dict_list(cursor)


        if len(rows) > 1:
            raise Exception("")

    except Exception as exc:
        raise Exception('')
    finally:
        try:
            cursor.close()
            con.close()
        except Exception as exc:
            raise Exception('')

    return rows

这会导致线程并发吗?最好的设计模式是什么?

【问题讨论】:

  • 我对 APScheduler 不熟悉。它是否使用多个进程?还是同一个进程中有多个线程?你能相应地更新你的问题吗?
  • @AnthonyTuinga 你可以选择任何你想要的。我现在正在使用 60 个线程的 ThreadPool 进行测试。我正在更新我的问题。

标签: multithreading python-3.6 cx-oracle apscheduler


【解决方案1】:

如果您的进程中有多个线程,那么当您连接到数据库时,您应该确保启用线程模式,如下所示:

con = cx.connect(user=data[FORMAT_CFG_DB_USER], password=data[FORMAT_CFG_DB_PASSWORD], dsn=dsn, threaded=True)

否则,您将面临损坏内存或导致其他问题的真正风险。也许这是您问题的根源?

【讨论】:

  • 有趣,我不知道这个。我今天就试试这个!
  • 1个多星期后,它似乎可以正常工作:) 感谢您的回答
  • 太棒了!很高兴听见。 :-)
猜你喜欢
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
相关资源
最近更新 更多