Google의 git 저장소 중 하나에서 bigquery 삽입에 대한 작은 샘플 코드를 찾았습니다.
app.yaml이 표시되면이 코드가 스레드로부터 안전해야한다고 말하지만 클라이언트 라이브러리의 문서 ( https://developers.google.com/api-client-library/python/guide/thread_safety )에서 확인하는 경우 스레드로부터 안전하지 않아야합니다. 다음 코드가 스레드 안전 여부에 대해 약간 혼란 스럽습니까? 앱 엔진 표준 환경에서 실행 중입니다.
import pprint
from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
# Create the bigquery api client
service = build('bigquery', 'v2', credentials=credentials)
response = service.datasets().list(projectId='PROJECTID').execute()
pprint.pprint(response)
---- 업데이트 ---- Tim의 답변 후 코드를 다음과 같이 변경했습니다. 이제 좋을 것입니다.
import pprint
from googleapiclient.discovery import build
from oauth2client.contrib.appengine import AppAssertionCredentials
import httplib2
credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/bigquery')
# Create the bigquery api client
service = build('bigquery', 'v2')
def get():
# authorize http object with client credentials
http = credentials.authorize(httplib2.Http())
response = service.datasets().list(projectId='PROJECTID').execute(http=http)
pprint.pprint(response)
참조하는 문서를 읽으면 다음과 같이 말합니다.
google-api-python-client 라이브러리는 스레드로부터 안전하지 않은 httplib2 라이브러리 위에 빌드됩니다. 따라서 다중 스레드 응용 프로그램으로 실행중인 경우 요청하는 각 스레드에는 httplib2.Http ()의 고유 한 인스턴스가 있어야합니다.
그런 다음 계속해서이를 수행하는 방법을 보여줍니다. 지침을 따르면 예입니다.
샘플 코드가 너무 간단하고 문서에 설명 된 것을 시도하지 않습니다.
# Create a new Http() object for every request
def build_request(http, *args, **kwargs):
new_http = httplib2.Http()
return apiclient.http.HttpRequest(new_http, *args, **kwargs)
service = build('api_name', 'api_version', requestBuilder=build_request)
# Pass in a new Http() manually for every request
service = build('api_name', 'api_version')
http = httplib2.Http()
service.stamps().list().execute(http=http)
따라서 스레드 상황에서 코드를 시도하면 스레드로부터 안전하지 않습니다. REPL에서 해당 코드를 시도하고 있다면 스레드 상황에있는 것 같습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다