![](/img/trans.png)
[英]`requests` returning different results for the same script if run both locally and on the cloud
[英]how to invoke the same cloud run from a cloud run to run requests parallely?
我正在使用雲運行運行 ETL 流程。
我有2000個文件。 只有 1200 個文件在 BIG Query 中得到預處理和加載。 因為雲運行正在超時。 所以,我想到了分配負載。
我將 2000 個文件分成 4 個,每個 500 個,並驗證和使用 requests.post 來調用相同的雲運行。 但是,它使用相同的雲運行實例依次執行一組。 它再次超時
我怎樣才能讓它並行運行?
截至目前,最大實例數:20。並發數:1,CPU:2,Memory:8GB。
好吧,我做過這樣的事情。 我不確定它是否對您有幫助,因為您沒有共享一個代碼塊。 這是我要下載的 2k JSON 文件的示例代碼。
您有 2000 個文件,其中 1200 個在雲運行超時之前在 GBQ 中得到處理/加載。 你可以做的是:
total_files = len(file_list)//1000 #let file list be 2000, total files will be 2.
#divide the files into sets of 1000 and loop over them one by one
for file_set in range(1,(total_files+1)):
auth_and_trigger(file_list[(file_set-1)*1000:(file_set*1000)])
#for files left after 1000*i , we finally trigger it.
auth_and_trigger(file_list[(total_files)*1000:len(file_list)])
現在,您可以使用 auth 調用雲運行並為每 1000 個文件觸發 function。
def auth_and_trigger(self, rest_of_files):
#your cloud run url
receiving_service_url = 'https://cloudrun-url-uc.a.run.app/download'
# Set up metadata server request
# See https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature
metadata_server_token_url = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience='
token_request_url = metadata_server_token_url + receiving_service_url
token_request_headers = {'Metadata-Flavor': 'Google'}
# Fetch the token
token_response = requests.get(token_request_url, headers=token_request_headers)
jwt = token_response.content.decode("utf-8")
# Provide the token in the request to the receiving service
receiving_service_headers = {'Authorization': f'bearer {jwt}'}
try:
threading.Thread(target=self.trigger_ingest,
args=(receiving_service_url,
{"files": rest_of_files},
receiving_service_headers
)).start()
except Exception as error:
logging.error(error)
每個線程都會調用一個 function trigger_ingest 來調用雲運行。 它的代碼如下:
def trigger_ingest(url, json, headers=""):
service_response = requests.post(url=url,
json=json,
headers=headers
)
logging.info(service_response.content)
現在,由於您想要並行執行,因此請確保線程中沒有代碼重復,因為您在雲運行的觸發器中擁有它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.