如何与Kivy GUI一起运行Tornado事件循环? [英] How to run the Tornado event loop alongside a Kivy GUI?

查看:95
本文介绍了如何与Kivy GUI一起运行Tornado事件循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户端应用程序使用Kivy GUI(Kivy具有其自己的事件循环),并使用带有Tornado的WebSocket协议连接到服务器(Tornado也具有事件循环).这就是连接部分是异步的原因. 我希望用户在Tornado客户端运行监听服务器消息的无限异步循环时与UI进行交互.

My client application uses a Kivy GUI (Kivy has its own event loop) and connects to the server using the WebSocket protocol with Tornado (Tornado also has an event loop). That's why the connection part is asynchronous.
I want the user to interact with the UI while a Tornado client is running an infinite asynchronous loop of listening for server messages.

下面是一些示例代码:
client_test.py

Here's some example code:
client_test.py

from tornado.ioloop import IOLoop
from tornado.websocket import websocket_connect

class RequestSender:
    url = 'server url here (no scheme)'

    async def _connect(self):
        self.conn = await websocket_connect('wss://' + self.url, io_loop=self.ioloop)
        self.ioloop.add_callback(self._listen)

    async def _listen(self):
        while True:
            print(await self.conn.read_message())

    def __init__(self):
        self.ioloop = IOLoop.current()
        self.ioloop.add_callback(self._connect)

    def run(self):
        self.ioloop.start()

GUI

from kivy.app import App
from kivy.uix.label import Label
from client_test import RequestSender

class TestApp(App):
    def build(self):
        RequestSender().run()
        return Label(text = "hello")

TestApp().run()

显然,由于Tornado的事件循环较早开始,它已经接管了程序流,现在没有GUI窗口出现. 我执行了GUI文件,并且执行在RequestSender().run()之后挂起,因此build从不返回.

Apparently, since the Tornado's event loop has started earlier, it has taken over the program flow and now no GUI window appears.
I execute the GUI file and the execution hangs after the RequestSender().run(), so build never returns.

在此案中进行搜索几乎没有提供任何信息,除了此Google网上论坛帖子. Kivy的文档仅提及扭曲.

Searching on this case provided little to no information, except for this Google Groups post. Kivy's documentation only mentions Twisted.

我尝试将Kivy事件循环置于从属模式,并从Tornado的事件循环中运行GUI更新,但这没有用,因为对Kivy事件循环的调用EventLoop.idle()显然不足以保持GUI应用程序运行.

I tried putting the Kivy event loop into slave mode and running GUI updates from Tornado's event loop, but that didn't work because apparently a call EventLoop.idle() of Kivy's event loop isn't enough to keep the GUI application running.

在这里还能做什么?

推荐答案

我发现这个问题试图做同样的事情,而是选择了两个单独的进程.一个Kivy GUI和一个Tornado(在我的情况下是服务器).正如此SO答案

I found this question trying to do the same thing, and opted for two separate processes instead; one Kivy GUI and one Tornado (Server, in my case). I have the two communicate using multiprocessing.connection as explained well in this SO answer

如果您要在两者之间传递庞大而复杂的数据,这可能不理想,但是对于简单的消息,它会很好地工作.您还具有以下优势:无需使用UI即可运行,并且可以在单独的计算机上运行UI.

If you have large and complex data to pass between the two, perhaps this is less than ideal, but for simple messages it works well. You also have the advantage of running without the UI, and running the UI on a separate machine.

这篇关于如何与Kivy GUI一起运行Tornado事件循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆