本文介绍了如何为 apscheduler 指定“记录器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用 Python 的 apscheduler 包,但它会定期抛出以下错误:

找不到记录器apscheduler.scheduler"的处理程序

此消息似乎与调度作业中的错误有关,例如使用jobTester作为调度作业,以下代码在jobTester中使用未定义的变量(nameStr0)给出了上述错误消息:

from apscheduler.scheduler import Scheduler从 apscheduler.jobstores.shelve_store 导入 ShelveJobStore从日期时间导入日期时间,时间增量从 schedJob 导入 toyJobdef jobTester(nameStr):outFileName = nameStr0 + '.txt'outFile = open(outFileName,'w')outFile.write(nameStr)outFile.close()def schedTester(jobList):调度程序 = 调度程序()scheduler.add_jobstore(ShelveJobStore('example.db'),'shelve')refTime = datetime.now()对于索引,枚举中的 currJob(jobList):运行时间 = refTime + timedelta(seconds = 15)jobName = currJob.name + '_' + str(index)scheduler.add_date_job(jobTester, runTime, name = jobName,jobstore = '搁置',args = [jobName])调度程序.start()stopTime = datetime.now() + timedelta(seconds = 45)print "开始等待循环......",而停止时间 >日期时间.现在():经过打印完成"定义 doit():姓名 = ['艾伦','芭芭拉','查理','达娜']jobList = [toyJob(n) for n in name]schedTester(jobList)

通过运行如下代码(存储在文件 schedTester.py 中)可以看到这一点:

>>>导入计划测试器>>>schedTester.doit()找不到记录器apscheduler.scheduler"的处理程序开始等待循环......完成

但是,当我将 nameStr0 替换为 nameStr(即变量名的正确拼写)时,代码运行良好,没有错误消息.

  1. 如何为 apscheduler.scheduler 创建记录器?我在处理配置调度程序的文档部分遗漏了什么

  2. 我认为这个记录器是某种标准错误是否正确?如果是这样,我将在哪里寻找它(如果这不是由我设置的方式决定的)

解决方案

你可以只创建一个默认的记录器,一切都应该去它:

导入日志logging.basicConfig()

只有在使用未定义的变量时才会出现问题的原因是,这会导致 jobTester 函数抛出一个错误,apscheduler 正在捕获该错误并尝试写入该错误带有 logging.error() 的消息.由于您还没有设置记录器,它会抱怨.

如果您阅读了 python 日志记录,您将看到有很多方法可以配置它.您可以让它将所有内容记录到文件中或将其打印到标准输出.

I'm trying to learn how to use Python's apscheduler package, but periodically, it throws the following error:

No handlers could be found for logger "apscheduler.scheduler"

This message seems to be associated with errors in the scheduled jobs, for example, using jobTester as the scheduled job, the following code, which uses an undefined variable (nameStr0) in jobTester gives the above error message:

from apscheduler.scheduler import Scheduler
from apscheduler.jobstores.shelve_store import ShelveJobStore
from datetime import datetime, timedelta
from schedJob import toyJob

def jobTester(nameStr):
    outFileName = nameStr0 + '.txt'
    outFile = open(outFileName,'w')
    outFile.write(nameStr)
    outFile.close()

def schedTester(jobList):
    scheduler = Scheduler()
    scheduler.add_jobstore(ShelveJobStore('example.db'),'shelve')
    refTime = datetime.now()

    for index, currJob in enumerate(jobList):
        runTime = refTime + timedelta(seconds = 15)
        jobName = currJob.name + '_' + str(index)
        scheduler.add_date_job(jobTester, runTime, name = jobName,
                           jobstore = 'shelve', args = [jobName])


    scheduler.start()
    stopTime = datetime.now() + timedelta(seconds = 45)
    print "Starting wait loop .....",
    while stopTime > datetime.now():
        pass
    print "Done"

def doit():
    names = ['Alan','Barbara','Charlie','Dana']
    jobList = [toyJob(n) for n in names]
    schedTester(jobList)

This may be seen by running this code (stored in the file schedTester.py) as follows:

>>> import schedTester
>>> schedTester.doit()
No handlers could be found for logger "apscheduler.scheduler"
Starting wait loop ..... Done

However, when I replace nameStr0 with nameStr (i.e. proper spelling of variable name), the code runs fine without the error message.

  1. How do I create a logger for apscheduler.scheduler? Am I missing something in the section of the docs dealing with configuring the scheduler

  2. Am I correct in thinking of this logger as some sort of a stderr ? If so, where will I look for it (if that is not determined by the way I set it up)

解决方案

You can just create a default logger and everything should go to it:

import logging
logging.basicConfig()

The reason that you only have a problem when you use a variable that hasn't been defined is that this causes the jobTester function to throw an error which apscheduler is catching and trying to write the error message with logging.error(). Since you haven't setup the logger it is going to complain.

If you read up on python logging you will see that there are many ways to configure it. You could have it log everything to a file or print it to stdout.

这篇关于如何为 apscheduler 指定“记录器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 09:37