问题描述
我一直收到以下错误:
socket.timeout: The read operation timed out
它发生在各种 Gmail API 请求期间,如下所示。
response = service.users().threads().get(userId='me',id=threadID,fields='messages(id)').execute()
或
或
att = service.users().messages().attachments().get(userId=userID,messageId=messageID,id=att_id).execute()
我提出了几个不同的 Gmail API 请求,这些只是其中的一部分。我相信他们中的大多数最终都会抛出这个错误。
我尝试将超时时间增加到 600 秒。以下是我用于批量请求的 http 对象。
def build_http(self):
"""Builds httplib2.Http object
Returns:
A httplib2.Http object,which is used to make http requests,and which has timeout set by default.
To override default timeout call
socket.setdefaulttimeout(timeout_in_sec)
before interacting with this method.
"""
try:
#DEFAULT_HTTP_TIMEOUT_SEC = 600
#if socket.getdefaulttimeout() is not None:
# http_timeout = socket.getdefaulttimeout()
#else:
# http_timeout = DEFAULT_HTTP_TIMEOUT_SEC
return httplib2.Http(timeout=600)
except:
self.GLogger.error("An error was encountered in build_http")
tb = traceback.format_exc()
self.GLogger.exception(tb)
return False
和
socket.setdefaulttimeout(600)
不清楚为什么会发生这种情况。有谁知道我可以做些什么来解决这些错误?它们发生得相当频繁。
def gmailAPIInitialize(self):
try:
self.waitForInternet()
self.GLogger.info("Initializing the Gmail API Service")
creds = None
# The file token.pickle stores the user's access and refresh tokens,and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle','rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available,let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json',ScopES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle','wb') as token:
pickle.dump(creds,token)
#service = build('gmail','v1',credentials=creds,cache_discovery=False)
service = build('gmail',cache_discovery=False)
self.gmailAPIService = service
self.GLogger.info("Successfully initialized the Gmail API Service")
return True
except:
self.GLogger.error("An error was encountered while attempting to initialize the Gmail API")
tb = traceback.format_exc()
self.GLogger.exception(tb)
return False
我还在运行 Gmail API 服务的两个实例。这两个实例中的每一个都使用一组不同的变量分别运行此函数。 这两个实例同时执行类似的 API 请求。这是一个问题吗?他们正在连接到同一个帐户。
我最初不认为这会是一个问题,因为我相信一个帐户可能有多个 Gmail API 实例,例如不同的电子邮件应用程序、电子邮件客户端等。
编辑* 所以我刚刚为同一个帐户创建了两个不同的凭据。我认为这可能有助于将两个实例及其提取分开。 Edit* socket.timeout 错误仍然发生。
编辑* 删除了对同一帐户执行相同请求(使用不同凭据)的另一个 Gmail API 线程。 socket.timeout 错误似乎不再发生。 这不允许吗?我有两个线程在执行相同的操作(略有不同),并为这两个处理同一电子邮件帐户的线程分配了两个不同的凭据。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)