如何在googlemlapipython客户端设置请求超时?

How to set the request timeout in google ml api python client?

我正在 运行 使用 google api python 客户端和模型对 google 云机器学习 API 进行在线预测在 google 云端为我托管。 当我预测发送一张图片时,服务器(包括所有流量)大约需要 40 秒。当我发送两张图片时,一段时间后,我收到消息:

timeout: The read operation timed out

我想将超时设置为其他值,但我没有找到如何设置。

这是我的代码:

import base64
import io
import time
from PIL import Image    

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery

SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
SERVICE_ACCOUNT_FILE = 'mycredentialsfile.json'

credentials = ServiceAccountCredentials.from_json_keyfile_name(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

ml = discovery.build('ml', 'v1', credentials=credentials)

projectID = 'projects/{}'.format('projectID') + '/models/{}'.format('modelID')

width = 640
height = 480

instances = []

for image in ["image5.jpg", "image6.jpg"]:
    img = Image.open(image)
    img = img.resize((width, height), Image.ANTIALIAS)
    output_str = io.BytesIO()
    img.save(output_str, "JPEG")
    instance = {"b64": base64.b64encode(output_str.getvalue()).decode("utf-8") }
    output_str.close()
    instances.append(instance)  

input_json = {"instances": instances }

request = ml.projects().predict(body=input_json, name=projectID)

print("Starting prediction")
start_time = time.time()
response = request.execute()

print("%s seconds" % (time.time() - start_time))

我找到了一种方法来研究 google api python 客户端 github 上的样本并尝试相同的更改。

使用httplib2认证可以设置超时。

修改后的代码如下:

import base64
import io
import time
from PIL import Image

# Need: pip install google-api-python-client

import httplib2
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery

SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
# API & Services -> Credentials -> Create Credential -> service account key
SERVICE_ACCOUNT_FILE = 'mycredentialsfile.json'

credentials = ServiceAccountCredentials.from_json_keyfile_name(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

http = httplib2.Http(timeout=200)
http = credentials.authorize(http)

ml = discovery.build('ml', 'v1', http=http)

projectID = 'projects/{}'.format('projectID ') + '/models/{}'.format('modelID')

width = 640
height = 480

instances = []

for image in ["image5.jpg", "image6.jpg"]:
    img = Image.open(image)
    img = img.resize((width, height), Image.ANTIALIAS)
    output_str = io.BytesIO()
    img.save(output_str, "JPEG")
    instance = {"b64": base64.b64encode(output_str.getvalue()).decode("utf-8") }
    output_str.close()
    instances.append(instance)  

input_json = {"instances": instances }

request = ml.projects().predict(body=input_json, name=projectID)

print("Starting prediction")
start_time = time.time()
response = request.execute()

print("%s seconds" % (time.time() - start_time))

我认为通过一些修改,您可以使用它为 python 客户端中的几乎任何 google 云 API 设置超时。

希望对您有所帮助。

你已经解决了这个问题,但我找到了另一种方法。

import socket    
socket.setdefaulttimeout(150)

如果在没有http的情况下调用discovery.build,http客户端由build_httpbuild方法中实例化。

https://googleapis.github.io/google-api-python-client/docs/epy/googleapiclient.http-pysrc.html#build_http

正如您在此处看到的,build_http 如果在创建 http 客户端之前设置了超时,则创建一个带超时的 http 客户端实例。

所以你所要做的就是通过socket.setdefaulttimeout设置这个值:)

是的。我同意上面Shohei的回答。我花了一段时间才找到这个简单而优雅的解决方案。您只需要在代码中添加以下内容

import socket
timeout_in_sec = 60*3 # 3 minutes timeout limit
socket.setdefaulttimeout(timeout_in_sec)

# then you could create your ML service object as usually, and it will have the extended timeout limit.
ml_service = discovery.build('ml', 'v1')