Python socketio,在后台运行socketio服务器

发布于 2025-01-20 15:44:59 字数 1119 浏览 5 评论 0 原文

如何启动 SocketIO 服务器并让它在后台监听,同时继续在主线程上执行代码?

现在我正在使用包python-socketio。我还测试了使用 python-socketio 的flask-socketiohttps://python-socketio.readthedocs.io/en/latest/server。 html

我主要尝试的是使用 sio.start_background_task 启动服务器。

例如:

class SocketIOServer(socketio.AsyncNamespace):
  def __init__(self):
    super().__init__()
    self.sio = socketio.AsyncServer()
    self.sio.register_namespace(self)
    self.app = web.Application()
    self.sio.attach(self.app)
    self.task = None

  def run(self):
    web.run_app(self.app, port=8080)

  def start(self):
    self.task = self.sio.start_background_task(self.run)

我尝试了上述方法和多种变体,例如使用 Flask、Tornado 等。

更清楚地说,这基本上就是我想做的:

if __name__ == '__main__':
  # ...
  # start app, e.g. -> web.run_app(self.app, port=8080)
  # I want to continue to execute code here

我不完全理解一切是如何工作的,我是在问一个愚蠢的问题吗?

How can I start a SocketIO server and have it be listening in the background while I continue to execute code on the main thread?

Right now I am using the package python-socketio. I have also testedflask-socketio which using python-socketio anyway.
https://python-socketio.readthedocs.io/en/latest/server.html

What I've mostly tried is starting the server with sio.start_background_task.

For example:

class SocketIOServer(socketio.AsyncNamespace):
  def __init__(self):
    super().__init__()
    self.sio = socketio.AsyncServer()
    self.sio.register_namespace(self)
    self.app = web.Application()
    self.sio.attach(self.app)
    self.task = None

  def run(self):
    web.run_app(self.app, port=8080)

  def start(self):
    self.task = self.sio.start_background_task(self.run)

I tried the above and multiple variations like using Flask, Tornado, etc.

To be more clear this is basically what I want to do:

if __name__ == '__main__':
  # ...
  # start app, e.g. -> web.run_app(self.app, port=8080)
  # I want to continue to execute code here

I don't fully understand how everything is working, am I asking a stupid question?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2025-01-27 15:44:59

正如您所描述的那样。您不完全了解一切的运作方式。

您缺少的最重要的是Python-Socketio软件包不是Web服务器。该软件包只会实现socket.io逻辑,但是您必须添加一个可以通过socket.io代码的Web服务器。

从您的问题中包含的代码中,您似乎已经选择将socket.io代码连接到使用AIOHTTP框架构建的Web应用程序,并使用自己的Web服务器。正确的?

然后,您遇到的真正问题是如何以非阻滞方式运行AIOHTTP Web服务器。

而且已经用AIOHTTP在其文档中具有有关此信息的信息:。他们这样做的例子:

runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', 8080)
await site.start()  # <-- starts the server without blocking

# you can do whatever you want here!

The problem is as you described it. You do not fully understand how everything works.

The most important thing that you are missing is that the python-socketio package is not a web server. This package just implements the Socket.IO logic, but you have to add a web server through which your Socket.IO code is available.

From the code that you included in your question, it appears that you have selected to attach the Socket.IO code to a web application built with the aiohttp framework, and use its own web server. Correct?

Then the real question that you have is how to run the aiohttp web server in a non-blocking way.

And it runs out that aiohttp has information on this in their documentation: https://docs.aiohttp.org/en/stable/web_advanced.html#application-runners. The example they have does this:

runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', 8080)
await site.start()  # <-- starts the server without blocking

# you can do whatever you want here!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文