1 回答

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())
添加回答
舉報(bào)