这是一个非常困难的问题,它是客户端服务器,所以我很抱歉只是客户端代码。也许有人能在这方面给我一些指导。
首先,在客户机中有一个名为listen的异步函数。这个函数读取来自服务器的输入,并预先形成一个动作。当前的问题是,当RPGMessage消息以非弹出的形式出现时,它会在没有错误的情况下使应用程序崩溃,只是“将应用程序留在进程中”。也许我可以在Kivy中扩展一些调试?
async def listen():
while True:
for message in CLIENT.get_reader_queue():
message = loads(message, cls=CustomJSONDecoder)
if isinstance(message, RPGMenu):
CLIENT_SCREEN.set_current_menu(message)
CLIENT_SCREEN.refresh()
if message.description is not None:
CLIENT_SCREEN.add_text_item(message.description)
elif isinstance(message, RPGMessage):
if message.popup is False:
CLIENT_SCREEN.add_text_item(message.text, message.font) <-- THIS IS THE LINE IT WILL TRIGGER.
else:
layout = BoxLayout(orientation='vertical')
layout.add_widget(Label(text=message.text))
button = Button(text='close', size_hint_y=0.25)
layout.add_widget(button)
popup = Popup(title='Server Message',
content=layout,
size_hint=(None, None), size=(400, 400), auto_dismiss=False)
button.bind(on_press=popup.dismiss)
popup.open()
elif isinstance(message, RPGMedia):
PopupWindow(message.name, message.text, message.image)
elif isinstance(message, RPGResultFailure):
PopupWindow(message.name, message.description)
elif isinstance(message, RPGResult):
CLIENT.result = message
await asyncio.sleep(0.1)无论如何,上面调用的CLIENT_SCREEN.add_text_item()如下所示;
def add_text_item(self, text: str, font: str=None):
text_input = Label()
text_input.padding_x = 10
text_input.size_hint_y = None
text_input.markup = True
text_input.text = text + '\n'
if font is not None:
text_input.font_name = font
text_input.font_size = '20sp'
text_input.bind(size=self.on_size_text_label)
self.text_grid_layout.add_widget(text_input)
self.text_scroll_view.scroll_to(text_input)
text_input.bind(on_ref_press=self.on_reference_press)这也是我要开始的循环;
if __name__ == '__main__':
loop = asyncio.get_event_loop()
# loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
CLIENT = Client('127.0.0.1', 9000, loop)
executer = ThreadPoolExecutor(2)
# asyncio.ensure_future(test())
# asyncio.ensure_future(console())
asyncio.ensure_future(listen())
# MyApp().run()
loop.run_in_executor(executer, MyApp().run)
loop.run_forever()当我运行调试器时,它会添加文本项,然后返回到异步循环,但是kivy死了。
有什么想法吗?我知道这是一个很难传达的问题。
谢谢。
更新;我已经尝试增加调试-但对此问题没有帮助;
Config.set('kivy', 'log_level', 'debug')发布于 2018-06-23 11:33:34
我已经找到了解决这个问题的办法。
为了让我混合Kivy和异步,我能够在一个附加到Kivy应用程序的线程中启动我的异步循环。这似乎可以彻底解决这一问题。
class MyApp(App):
def build(self):
from kivy.core.window import Window
sm = ScreenManager()
sm.transition = NoTransition()
login_screen = LoginScreen(name='login')
client_screen = ClientScreen(name='client')
sm.add_widget(login_screen)
sm.add_widget(client_screen)
sm.current = 'login'
Window.size = (300, 120)
self.title = 'xNemesis Client V0'
asyncio.ensure_future(listen(client_screen))
x = threading.Thread(target=loop.run_forever)
x.start()
return sm因此,构建了应用程序,就在最后,我启动了异步循环的线程。那样的话,我就不用全局变量了,我认为这是不喜欢的。
https://stackoverflow.com/questions/50949127
复制相似问题