重复的瞬态套接字连接

编程入门 行业动态 更新时间:2024-10-27 07:30:28
重复的瞬态套接字连接 - 内存泄漏风险?(Repeated transient-socket connections - memory leak risk?)

我正在编写一个脚本,打开一个文本文件并循环遍历每一行(在每一行之间暂停几秒钟)。 对于每一行,它将打开一个临时客户端套接字连接,并将文本发送到主机服务器。 主持人的回应可能会也可能不会; 无论哪种方式都没关系。

我已经遇到了Python套接字限制,你无法重新连接现有的套接字对象(因为这样做会触发异常EBADF, 'Bad file descriptor' )。 所以我正在为每个瞬态连接创建一个新的套接字实例。 当然,诀窍就是如何避免内存泄漏。

我接近这个的方法是将创建,使用和关闭套接字的整个部分推送到一个函数 - 依赖Python的垃圾收集来删除每个实例后:

import socket,select,time def transientConnect(host,port,sendData): response = '' sendSocket = socket.socket() sendSocket.connect((serverHost,serverPort)) sendSocket.send(line) gotData = select.select([sendSocket],[],[],2) if (gotData[0]):response = sendSocket.recv(65535) sendSocket.close() return response scriptLines = open('testScript.txt','r').readlines() serverHost = '127.0.0.1' serverPort = 15004 for line in scriptLines: response = transientConnect(serverHost,serverPort,line) print(response) time.sleep(3.0)

我的问题:(1)这种方法是否可以避免任何内存泄漏? (2)有没有更直接的方法来确保每个实例在完成后都被淘汰了?

I'm writing a script that opens a text file and loops through each line (pausing a few seconds between each one). For each line, it opens a transient client socket connection and sends the text to a host server. A host response may or may not come; it doesn't matter either way.

I've already bumped into the Python socket limitation where you can't reconnect with an existing socket object (because doing so triggers the exception EBADF, 'Bad file descriptor'). So I'm creating a new socket instance for each transient connection. The trick then of course becomes how to avoid a memory leak.

The way I've approached this is to push the whole portion of creating, using, and closing the socket to a function -- relying on Python's garbage collection to remove each instance after I'm done with it:

import socket,select,time def transientConnect(host,port,sendData): response = '' sendSocket = socket.socket() sendSocket.connect((serverHost,serverPort)) sendSocket.send(line) gotData = select.select([sendSocket],[],[],2) if (gotData[0]):response = sendSocket.recv(65535) sendSocket.close() return response scriptLines = open('testScript.txt','r').readlines() serverHost = '127.0.0.1' serverPort = 15004 for line in scriptLines: response = transientConnect(serverHost,serverPort,line) print(response) time.sleep(3.0)

My questions: (1) Will this approach work to avert any memory leaks? (2) Is there a more direct way to make sure each instance is eliminated after I'm finished with it?

最满意答案

首先,仅使用套接字进行单次交换是正常的。 请参阅插座HOWTO 。

关于python的一个好处是,通常你不必担心垃圾收集。 除非你有真正的内存使用问题,否则你不应该这样做。

在此网页上 ,请注意:

“当一个对象超出范围时,Python不会清理它。当它最后一次引用超出范围时,它会清理它。”

因此,如果在函数内部创建的套接字未在其他地方引用,则它应该超出范围并被释放(但不是gc-ed)。 以下内容可能与cpython有关。 阅读gc.set_threshold()的文档,了解垃圾收集在cpython中的工作原理。 特别:

“当分配数量减去解除分配数量超过阈值0时 ,开始收集。”

阈值的标准值(以cpython为单位)为:

In [2]: gc.get_threshold() Out[2]: (700, 10, 10)

因此,在获得gc运行之前,会有相当多的分配。 您可以通过运行gc.collect() 强制进行垃圾回收。

First off, it is normal to only use a socket for a single exchange. See the socket HOWTO.

One of the nice things about python is that in general you don't have to worry about garbage collection. And you shouldn't unless you have real memory use problems.

From this webpage, keep in mind that:

"Python won’t clean up an object when it goes out of scope. It will clean it up when the last reference to it has gone out of scope."

So if the socket created inside the function isn't referenced elsewhere, it should go out of scope and be deallocated (but not gc-ed). What follows is probably specific to cpython. Read the documentation of gc.set_threshold() for an idea how garbage collection works in cpython. Especially:

"When the number of allocations minus the number of deallocations exceeds threshold0, collection starts."

The standard values for the thresholds (in cpython) are:

In [2]: gc.get_threshold() Out[2]: (700, 10, 10)

So there would heva to be a fair number of allocations before you get a gc run. You can force garbage collection by running gc.collect().

更多推荐

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

发布评论

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

>www.elefans.com

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