min*_*ins 4 python kivy python-asyncio
迷失在 asyncio 中。
我正在同时学习 Kivy 和 asyncio,并且陷入了解决运行 Kivy 和运行 asyncio 循环的问题上,因为无论我如何转动它,两者都是阻塞调用并且需要按顺序执行(好吧,我希望我错了),例如
loop = asyncio.get_event_loop()
loop.call_soon(MyAsyncApp().run())
loop.run_forever()
Run Code Online (Sandbox Code Playgroud)
我当前的尝试导致应用程序启动,但没有执行协程,例如,当我单击“连接”按钮时,我应该使用 开始调度和执行任务loop.call_soon,但没有任何反应。
有人可以看看我的代码并提出解决问题的正确方法吗?
import asyncio
import random
import time
from kivy.app import App
from kivy.lang import Builder
ui = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
GridLayout:
rows: 2
cols: 2
Label:
text: 'Status:'
size_hint: 0.3, 1
Label:
id: status
text: ''
Label:
text: 'Data:'
size_hint: 0.7, 1
Label:
id: data
text: ''
BoxLayout:
direction: 'horizontal'
Button:
text: 'Get Data'
on_press: app.connect()
Button:
text: 'Stop Data'
on_press: pass
''')
class MyAsyncApp(App):
def __init__(self):
super(self.__class__, self).__init__()
self.x_connected = None
self.x_widget_data = None
self.x_widget_status = None
self.x_loop = asyncio.get_event_loop()
def build(self):
return ui
def connect(self):
# Get widget
self.x_widget_status = self.root.ids.status
# Update status
self.x_widget_status.text = 'Preparing to connect...'
# Connect using asyncio
# --> But the loop must be already running <---
self.x_loop.call_soon(self.do_connect)
async def do_connect(self):
# Connect asynchronously
# Get widget
self.x_widget_data = self.root.ids.data
# Update status
self.x_connected = False
self.x_widget_status.text = 'Connecting...'
# Perform actual actions
try:
result = await self.feed_kivy()
if result:
self.x_widget_status.text = 'Service not available: ' + result
return
except Exception as e:
self.x_widget_status.text = 'Error while connecting'
return
# Update status
self.x_connected = True
self.x_widget_status.text = 'Connected'
async def feed_kivy(self):
# Deliver fresh data at random interval
# Some slow process to get data
result = await asyncio.sleep(random.randint(1, 5), time.time())
self.x_widget_data.text = result
# Reschedule ourselves
await self.x_loop.call_soon(self.feed_kivy())
def main():
# If loop started here, app is never started
loop = asyncio.get_event_loop()
loop.call_soon(MyAsyncApp().run())
loop.run_forever()
loop.close()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
我当前的尝试导致应用程序启动,但没有执行协程
这是因为MyAsyncApp().run()块执行流和控制永远不会返回到 asyncio 的事件循环。这就是所有事件循环的工作方式。
而不是手动尝试跨两个循环更短的方法是使用现有的尝试:
https://github.com/kivy/kivy/pull/5241
此 PR 来自 Kivy 的一位开发人员,包含带有解释和使用示例的工作实现。
但是它尚未合并到 master 中:您需要手动使用此 PR 构建 Kivy。