多线程Python套接字服务器CPU使用率急剧失控

发布于 2024-12-04 06:34:09 字数 1298 浏览 1 评论 0原文

我的情况:

我有一个Python服务器,它执行以下操作:

侦听端口500(出于防火墙原因),每次收到连接时,都会生成一个线程来处理该连接。启动的每个线程都将使用一些主要是数据库交互的方法来响应客户端输入(我实际上使用 Django ORM,因为我的服务器应用程序与 Django 网站耦合)。

我想在某个时候使用 select(最终是 Twisted),但现在我不能,所以我必须这样做。


我的问题:

出于某种我似乎无法理解的原因,服务器的 CPU 使用率有时会完全失控,CPU 使用率高达 200%(我们在双核上运行),这使得通过 ssh 来阻止它变得非常困难。

我不明白的是,它通常不会在操作期间发生(如果我连接了一个或多个客户端,CPU 使用率保持非常 低),但是一旦所有客户端都断开连接,我的服务器就会停止CPU 使用率高达 200%。

这让我相信问题不在于工作线程(如果它们没有正常终止,我宁愿期望大量 RAM 使用而不是 CPU 线程),而在于服务器的接受方法。

到目前为止,我一直在使用这段代码:

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen(5)
logger.warning('Started - listening on {0}:{1}'.format(host, port))

while True:
    (newsocket, clientaddr) = s.accept() 
    logger.info('Received connection from {0}:{1}'.format(clientaddr[0], clientaddr[1]))
    WorkerThread(newsocket, clientaddr, timeout).start()

我无法真正理解这里可能出了什么问题,但我认为也许我应该使用以下语法:

while True:
    (newsocket, clientaddr) = s.accept() 
    logger.info('Received connection from {0}:{1}'.format(clientaddr[0], clientaddr[1]))
    wk = WorkerThread(newsocket, clientaddr, timeout)
    wk.start()

我看到这样写的次数比我写的要多得多到目前为止已经做了。

你们中有人知道这是否会导致像我所描述的那样的问题吗?

提前致谢,

My Situation:

I have a python server that does the following:

Listens on port 500 (for firewall reasons), and everytime it receives a connection, spawns a thread to handle the connection. Each thread that is started will respond to client input with a few methods that are mostly database interaction (I actually use the Django ORM, as my server application is coupled with a Django website).

I'd like to use select at some point (and ultimately, Twisted), but right now, I can't, so i'll have to go with this.

My problem:

For some reason I can't seem to understand, the server's CPU usage sometime spirals totally out of control and goes up to 200% CPU usage (we're running on a dual core), making it even pretty difficult to ssh in to stop it.

What I don't understand is that it usually does not happen during operation (If I have one or multiple clients connected, the CPU usage stays very low), but once all clients have disconnected, my server goes up to 200% CPU usage.

This led me to believe that the problem is not in the worker threads (If they didn't die properly, I'd rather expect a massive RAM usage than a CPU one), but in the server's accept method.

Up to now, I've been using this code:

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen(5)
logger.warning('Started - listening on {0}:{1}'.format(host, port))

while True:
    (newsocket, clientaddr) = s.accept() 
    logger.info('Received connection from {0}:{1}'.format(clientaddr[0], clientaddr[1]))
    WorkerThread(newsocket, clientaddr, timeout).start()

I can't really grasp what could be going wrong here, but I thought that maybe I should use the following syntax:

while True:
    (newsocket, clientaddr) = s.accept() 
    logger.info('Received connection from {0}:{1}'.format(clientaddr[0], clientaddr[1]))
    wk = WorkerThread(newsocket, clientaddr, timeout)
    wk.start()

I've seen this written a lot more often than what I've doing up to now.

Does anyone of you knows whether this could cause a problem such as the one I'm describing?

Thanks in advance,

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文