第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

并行運(yùn)行 API 調(diào)用,仍然按順序運(yùn)行

并行運(yùn)行 API 調(diào)用,仍然按順序運(yùn)行

紅糖糍粑 2022-12-27 14:50:28
我正在調(diào)用一個(gè) API 來(lái)分析一些文件并返回?cái)?shù)據(jù)。我必須在幾百個(gè)文件上使用它,所以我想我可以讓調(diào)用并行運(yùn)行,因?yàn)槲募姆治龊徒Y(jié)果(要寫(xiě)入它自己的文件)彼此沒(méi)有關(guān)系。我的偽代碼是這樣的——從文件夾中獲取文件列表,為每個(gè)文件啟動(dòng)一個(gè)請(qǐng)求,等待響應(yīng)并寫(xiě)入相應(yīng)的文件。我寫(xiě)了下面的代碼,但它似乎仍然按順序運(yùn)行,而不是一次全部運(yùn)行。我究竟做錯(cuò)了什么 ?import osimport asyncioimport jsonimport timepath = "/home/repo/"result_path = "/home/repo/Results/"async def to_json(obj, file_name):    with open(result_path + file_name + ".json", "w", encoding="utf-8") as wr:        await json.dump(            obj, wr, ensure_ascii=False, indent=4, default=lambda obj: obj.__dict__        )class AnalyzeFile(object):    async def start_analyze_file(self, file_name):        endpoint = "https://api.com/"        key = "key"        print("Creating a recognizer client")        async with FileClient(endpoint=endpoint, key=key) as client:            with open(path + file_name, "rb") as f:                file = await client.analyze_file(model_id=model_id, stream=f.read())        file_result = await file.result()        print("Results are back for %s" % file_name)        print("Analyze ended at %s" % time.asctime(time.localtime(time.time())))        print("Writing to file")        await to_json(forms, file_name)        print("Done writing to file")async def main():    af = AnalyzeFile()    for file_name in os.listdir(path):        await sample.start_analyze_file(file_name)    print("done")if __name__ == "__main__":    loop = asyncio.get_event_loop()    loop.run_until_complete(main())
查看完整描述

1 回答

?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊

關(guān)鍵字async&awake在您使用它們的意義上可能不起作用。@background您需要為需要執(zhí)行的功能添加簽名。這樣它就可以并行運(yùn)行。并且只是那個(gè)功能。在你的情況下start_analyze_file()。如下:


def background(f):

    def wrapped(*args, **kwargs):

        return asyncio.get_event_loop().run_in_executor(None, f, *args, **kwargs)


    return wrapped


def to_json(obj, file_name):

    with open(result_path + file_name + ".json", "w", encoding="utf-8") as wr:

        await json.dump(

            obj, wr, ensure_ascii=False, indent=4, default=lambda obj: obj.__dict__

        )



class AnalyzeFile(object):

    @background    

    def start_analyze_file(self, file_name):


        endpoint = "https://api.com/"

        key = "key"


        print("Creating a recognizer client")

        with FileClient(endpoint=endpoint, key=key) as client:

            with open(path + file_name, "rb") as f:

                file = await client.analyze_file(model_id=model_id, stream=f.read())

        file_result = await file.result()

        print("Results are back for %s" % file_name)

        print("Analyze ended at %s" % time.asctime(time.localtime(time.time())))

        print("Writing to file")

        await to_json(forms, file_name)

        print("Done writing to file")



def main():

    af = AnalyzeFile()

    for file_name in os.listdir(path):

        af.start_analyze_file(file_name)

    print("done")



if __name__ == "__main__":

    loop = asyncio.get_event_loop()

    loop.run_until_complete(main())



查看完整回答
反對(duì) 回復(fù) 2022-12-27
  • 1 回答
  • 0 關(guān)注
  • 91 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)