英文:
Jupyter Notebook Async functions
问题
以下是您要翻译的内容:
在下面的代码中,我希望出现以下行为:
当用户单击我在App Mode中定义的按钮时,我希望其中的print_async例程被调用,它会等待2秒,然后打印“这是来自buttonCLicked的异步打印”,然后我希望出现“按钮已点击!”的打印。但实际上我得到了一个解释器错误:
任何帮助都将不胜感激。
**File "cell_name", line 6
SyntaxError: 'await' outside async function**
from IPython.display import display
from ipywidgets import widgets
import asyncio
#不要更改此单元格
#定义一个异步函数
async def print_async(message):
await asyncio.sleep(2)
print(message)
# 显示我们可以从顶级jupyter终端打印
await print_async("这是顶级异步打印")
#为按钮定义回调函数
def onButtonClicked(_):
await print_async("这是来自buttonCLicked的异步打印")
print("按钮已点击!")
button = widgets.Button(
description='点击我',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger'或''
tooltip='点击我',
icon=''
)
button.on_click(onButtonClicked)
display(button)
button = widgets.Button(
description='点击我',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger'或''
tooltip='点击我',
icon=''
)
button.on_click(onButtonClicked)
display(button)
目标:使按钮单击调用print_async方法
请注意,我已经将代码部分排除在翻译之外,只翻译了文本内容。
英文:
In the code below, I'd like the following behavior:
When the user clicks the button I've defined in App Mode, I want the print_async routine inside of it to be invoked, which waits for 2 seconds and then prints ""this is async print from buttonCLicked", then I want the print after which is "button clicked!" to appear. Instead what I get is an interpreter error:
Any help appreciated.
File "cell_name", line 6
SyntaxError: 'await' outside async function
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
答案1
得分: 1
你可以使用asyncio.run
函数来实现这个(需要Python 3.7或更新版本):
在你的代码中,它会像这样:
def onButtonClicked(_):
asyncio.run(print_async("这是来自按钮点击的异步打印"))
print("按钮被点击了!")
英文:
You can do this with the asyncio.run
function (Requires Python 3.7 or newer):
In your code it would look like this:
def onButtonClicked(_):
asyncio.run(print_async("this is async print from buttonCLicked"))
print("button clicked!")
答案2
得分: 0
首先,你不能在异步函数外部使用await关键字。如果这是你异步程序的入口点,可以使用asyncio.run()函数,并将要运行的协程函数作为参数传递给run()方法来调用该协程。
英文:
First of all, You cannot use await keyword outside async function. If that is the entry point to your asynchronous program, use asyncio.run() function with function call as a parameter to run() method to call that co-routine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论