Python asyncio添加其他功能
我正在尝试创建一个脚本,如果可能的话,它将使用asyncio连续运行代码Python asyncio添加其他功能,python,python-asyncio,Python,Python Asyncio,我正在尝试创建一个脚本,如果可能的话,它将使用asyncio连续运行代码 # parse_name.py import parse import asyncio async def parse_name(text: str) -> str: patterns = ( "my name is {name}", "i'm {name}", "i am {name}", "call me {name}",
# parse_name.py
import parse
import asyncio
async def parse_name(text: str) -> str:
patterns = (
"my name is {name}",
"i'm {name}",
"i am {name}",
"call me {name}",
"{name}",
)
for pattern in patterns:
result = parse.parse(pattern, text)
if result:
return result["name"]
return ""
answer = input("What is your name? ")
name = parse_name(answer)
print(f"Hi {name}, nice to meet you!")
async def main():
divs1 = loop.parse_name(text)
await asyncio.wait([divs1])
return divs1
if __name__ == "__main__":
try:
loop = asyncio.get_event_loop()
loop.set_debug(1)
d1 = loop.run_until_complete(main())
except Exception as e:
# logging...etc
pass
finally:
loop.close()
基本上这个脚本将打开一个命令提示符。。。但随后立即关闭。。名字提示出现了,我插入的东西没有通过诸如此类的东西,它也关闭了。有谁能告诉我这个脚本将在哪里持续运行?基本上在之后很高兴见到你!“
它会循环回到”你叫什么名字?“
win32上的Python 3.6.8(tags/v3.6.8:3C6B436A572018年12月24日00:16:47)[MSC v.1916 64位(AMD64)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证()。
>>>
=========重新启动:C:/Users/benb/Desktop/text_parse/loopTest.py===========
你叫什么名字?胡说
嗨,很高兴认识你!
>>>
如果您只想连续编写一些代码,只需将此代码放入中即可,而True:
asyncio
应用于特定任务,对于非IO函数通常不需要asyncio
。阅读它可能会有所帮助。@MikhailGerasimov,它实际上适用于我想用执行的所有操作,而True
如果你发布一个答案,我可以点击绿色复选框。谢谢
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=========== RESTART: C:/Users/benb/Desktop/text_parse/loopTest.py ===========
What is your name? blah blah
Hi <coroutine object parse_name at 0x000001A8F0526BF8>, nice to meet you!
>>>