11import argparse
2+ import asyncio
23import json
34import os
45import sys
56import time
67from datetime import datetime
78
9+ import aiohttp
810import jsonpickle
911import requests
1012from dotenv import find_dotenv , load_dotenv
@@ -100,7 +102,7 @@ def poll_job_status(job_id, interval=10):
100102 time .sleep (interval )
101103
102104
103- def main ():
105+ async def main ():
104106 parser = argparse .ArgumentParser (description = 'Sandbox job cli' )
105107 subparsers = parser .add_subparsers (
106108 dest = 'command' , help = 'Available commands' , required = True
@@ -138,7 +140,7 @@ def main():
138140 )
139141
140142 args = parser .parse_args ()
141-
143+ access_key = os . getenv ( 'MATERIALS_ACCESS_KEY' )
142144 # 调用API获取job信息
143145 logger .info (f"Job ID: { args .job_id } " )
144146
@@ -150,7 +152,7 @@ def main():
150152
151153 try :
152154 response = requests .get (
153- f"{ OpenAPIJobAPI } /{ args .job_id } ?accessKey={ os . getenv ( 'BOHRIUM_ACCESS_KEY' ) } "
155+ f"{ OpenAPIJobAPI } /{ args .job_id } ?accessKey={ access_key } "
154156 )
155157 response .raise_for_status ()
156158 job_info = response .json ()
@@ -181,23 +183,26 @@ def main():
181183 logger .info (f"{ job_name } [{ job_status } ] -- { duration } " )
182184
183185 # download log
184- get_token_and_download_file ('log' , args .job_id )
186+ await get_token_and_download_file ('log' , args .job_id , access_key )
185187
186188 if job_status in ['Running' ]:
187189 return
188190 elif job_status == 'Finished' :
189191 # download result.txt
190192 results_txt = 'results.txt'
191- get_token_and_download_file (results_txt , args .job_id )
193+ await get_token_and_download_file (results_txt , args .job_id , access_key )
192194 with open (results_txt ) as f :
193195 logger .info (jsonpickle .loads (f .read ()))
194196 os .remove (results_txt )
195197
196198 if args .download_output :
197199 # 下载结果文件
198200 if result_url and result_url != 'null' :
199- result_response = requests .get (result_url , stream = True )
200- check_status_and_download_file (result_response , args .output )
201+ async with aiohttp .ClientSession () as session :
202+ async with session .get (result_url ) as result_response :
203+ await check_status_and_download_file (
204+ result_response , args .output
205+ )
201206 else :
202207 logger .error ('No resultUrl found or resultUrl is empty' )
203208 elif args .command == 'kill' :
@@ -207,4 +212,4 @@ def main():
207212
208213
209214if __name__ == '__main__' :
210- main ()
215+ asyncio . run ( main () )
0 commit comments