Jupyter notebook Jupyter笔记本异步函数

Jupyter notebook Jupyter笔记本异步函数,jupyter-notebook,python-asyncio,Jupyter Notebook,Python Asyncio,在下面的代码中,我想要以下行为: 当用户单击我在应用程序模式中定义的按钮时,我希望调用其中的print\u async例程,该例程等待2秒,然后从buttonCLicked打印这是异步打印,然后我希望打印之后单击按钮!出现。相反,我得到的是一个解释器错误: 谢谢你的帮助 文件单元名称,第6行 SyntaxError:异步函数外部的“等待” 您可以使用需要Python 3.7或更新版本的函数执行此操作: 在您的代码中,它将如下所示: def ONButton点击按钮: asyncio.runpri

在下面的代码中,我想要以下行为:

当用户单击我在应用程序模式中定义的按钮时,我希望调用其中的print\u async例程,该例程等待2秒,然后从buttonCLicked打印这是异步打印,然后我希望打印之后单击按钮!出现。相反,我得到的是一个解释器错误:

谢谢你的帮助

文件单元名称,第6行 SyntaxError:异步函数外部的“等待”

您可以使用需要Python 3.7或更新版本的函数执行此操作:

在您的代码中,它将如下所示:

def ONButton点击按钮: asyncio.runprint\u async这是从按钮单击的异步打印 点击打印按钮!
我认为这遗漏了语法错误,如第6行,wait print_async这是一个顶级异步打印,它不正确,因为它不在共同例程defn中。删除它,我相信并发编程逻辑将是有效的。
from IPython.display import display
from ipywidgets import widgets
import asyncio
#DO NOT CHANGE THIS CELL
#Define an async function
async def print_async(message):
    await asyncio.sleep(2)
    print(message)
# Show that we can print from the top-level jupyter terminal
await print_async("This is a top-level async print")
#Define a callback function for the button
def onButtonClicked(_):
    await print_async("this is async print from buttonCLicked")
    print("button clicked!")

button = widgets.Button(
    description='Click me',
    disabled=False,
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Click me',
    icon=''
)

button.on_click(onButtonClicked)
display(button)
​
button = widgets.Button(
    description='Click me',
    disabled=False,
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Click me',
    icon=''
)
​
button.on_click(onButtonClicked)
display(button)
Goal: Get the button click to call the print_async method