异步添加其他功能

时间:2019-09-23 18:44:36

标签: 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}",
        "{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()

基本上,该脚本将打开命令提示符...,然后立即关闭。。出现名称提示符,我插入的内容不是通过blah blah来输入的,它也将关闭。谁能给我一个提示,这个脚本将继续运行?基本上在nice to meet you!"之后,它将循环回到"What is your name? "

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!
>>> 

0 个答案:

没有答案