所以报错这样是正常的对吗from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR import datetime import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%Y-%m-%d %:H%M%S', filename='log1.txt', filemode='a') logging.FileHandler(encoding='utf-8', filename='log1.txt') def aps_test(x): print(datetime.datetime.now().strftime('%Y-%m-%d %:H%M%S'), x) def date_test(x): print(datetime.datetime.now().strftime('%Y-%m-%d %:H%M%S'), x) print(1/0) def my_listener(event): if event.exception: print('出错??????') else: print('正常///////////') scheduler = BlockingScheduler() scheduler.add_job(func=date_test, args=('一次性出错任务',), next_run_time=datetime.datetime.now()+datetime.timedelta(seconds=15), id='date_task') scheduler.add_job(func=aps_test, args=('循环任务',), trigger='interval', seconds=3, id='interval_task') scheduler.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR) scheduler._logger=logging scheduler.start()
时间: 2023-07-16 13:14:16 浏览: 125
这个报错是因为在logging.basicConfig()方法中,有一个格式字符串的语法错误,即datefmt参数的格式字符串中有一个“%”符号后面没有跟任何字符,应该把它改成“%H”。正确的格式字符串应该是'%Y-%m-%d %H:%M:%S'。修改后的代码如下:
```python
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR
import datetime
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S', # 修改这里的格式字符串
filename='log1.txt',
filemode='a')
logging.FileHandler(encoding='utf-8', filename='log1.txt')
def aps_test(x):
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)
def date_test(x):
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)
print(1/0)
def my_listener(event):
if event.exception:
print('出错??????')
else:
print('正常///////////')
scheduler = BlockingScheduler()
scheduler.add_job(func=date_test, args=('一次性出错任务',),
next_run_time=datetime.datetime.now()+datetime.timedelta(seconds=15),
id='date_task')
scheduler.add_job(func=aps_test, args=('循环任务',), trigger='interval', seconds=3, id='interval_task')
scheduler.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
scheduler._logger=logging
scheduler.start()
```
注意:如果你在使用这段代码时,日期时间格式不需要秒数,可以将datefmt参数改为'%Y-%m-%d %H:%M'。
阅读全文