在 PyQt 应用程序中嵌入 aiohttp 服务器

编程入门 行业动态 更新时间:2024-10-25 18:24:52
本文介绍了在 PyQt 应用程序中嵌入 aiohttp 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我打算在 PyQt 应用程序中嵌入一个 aiohttp 服务器,但是当我运行下面的代码时,Qt 窗口无法显示,我知道这是由 web.run_app(app),我试图将它移动到一个线程中,但是我得到了 RuntimeError: There is no current event loop in thread 'Dummy-1',那么我该怎么办?我发现 asyncqt 这可能会有所帮助,但我不知道如何使用它来处理aiohttp 服务器.

I am going to embed a aiohttp server in a PyQt application, but when I run the code below , the Qt window couldn't show, I know it was caused by web.run_app(app), I've tried to move it into a thread , but then I got RuntimeError: There is no current event loop in thread 'Dummy-1', so what should I do ? I've found asyncqt which might help ,but I don't know how to use it to deal with a aiohttp server.

from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from aiohttp import web class ThreadGo(QThread): # threading.Thread # implementing new slots in a QThread subclass is error-prone and discouraged. def __init__(self, parent, func, *args, **kwargs): super().__init__(parent) self.func = func self.args = args self.kwargs = kwargs self.result = 0 onFinished = self.kwargs.get('onFinished') self.finished.connect(onFinished) if onFinished else None # 用lambda还不行呢 self.finished.connect(self.deleteLater) self.start() def run(self): self.result = self.func(*self.args) # deleteLater class Window(QMainWindow): def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.setUpHTTPServer() def setUpHTTPServer(self): async def hello(request): return web.Response(text="Hello, world") app = web.Application() app.add_routes([web.get('/', hello)]) web.run_app(app) # ThreadGo(self, lambda:web.run_app(app))#get RuntimeError: There is no current event loop in thread 'Dummy-1 if __name__ == "__main__": from sys import argv, exit a = QApplication(argv) w = Window() w.show() exit(a.exec_())

推荐答案

在下面的例子中,我展示了如何在 aiohttp 服务器上使用 Qt:

In the following example I show how to use Qt with the aiohttp server:

import asyncio from functools import cached_property from PyQt5.QtWidgets import QApplication, QMainWindow from asyncqt import QEventLoop from aiohttp import web class Window(QMainWindow): def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.setup_server() def setup_server(self): self.app.add_routes([web.get("/", self.hello)]) @cached_property def app(self): return web.Application() async def hello(self, request): return web.Response(text="Hello, world") def run(self): web.run_app(self.app) def main(): import sys a = QApplication(sys.argv) loop = QEventLoop(a) asyncio.set_event_loop(loop) w = Window() w.show() w.run() if __name__ == "__main__": main()

更多推荐

在 PyQt 应用程序中嵌入 aiohttp 服务器

本文发布于:2023-11-23 10:34:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   服务器   PyQt   aiohttp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!