问题描述
我正在尝试使用官方文档中几乎未更改的代码来读取相当大的日历。
from exchangelib import DELEGATE,Account,Credentials,CalendarItem,close_connections
from exchangelib.protocol import BaseProtocol
credentials = Credentials(
username='username',password='password'
)
account = Account(
primary_smtp_address='address',credentials=credentials,autodiscover=True,access_type=DELEGATE)
a = account
for calendar_item in a.calendar.all():
print('--------------------------')
print(calendar_item.organizer,calendar_item.start)
account.protocol.close()
与此同时,脚本的第一分钟没有任何反应。然后几百行输出是我需要的输出。之后,脚本因超时而停止,并出现以下错误: 引发 self._get_exception(code=response_code,text=msg_text,msg_xml=msg_xml) exchangelib.errors.ErrorTimeoutExpired:请求超时。
我能否以某种方式增加超时或更改请求以使其工作得更快?
解决方法
脚本在第一分钟并没有闲置。如果您enable debug logging,您很可能会看到自动发现需要很长时间。如果您想缩短启动时间,您可以 cache autodiscover results 并在下次运行脚本时使用它们。
至少有三种方法可以解决您的问题(甚至可以组合使用):
- 创建超时。设置例如。
BaseProtocol.TIMEOUT = 600
- 减少获取的数据量。不要
a.calendar.all()
,而是a.calendar.all().only('organizer','start')
- 配置 retry policy 以让 exchangelib 在遇到超时错误时重试。