Python docs这么说;
socket.makefile([mode[, bufsize]])
Return a file object associated with the socket. (File objects are described in File Objects.) The file object references a dup()ped version of the socket file descriptor, so the file object and socket object may be closed or garbage-collected independently.
这是否意味着我必须像这样关闭套接字和文件?
conn = socket.create_connection((addr,port))
myfile = conn.makefile()
myfile.close()
conn.close()
或者关闭一个就足够了?如果是这样,我关闭哪一个和不关闭哪一个有关系吗?
最佳答案
正如文档所述,两个对象都是独立的。仅当与其相关的所有文件描述符都关闭时,底层连接才会关闭。您必须关闭两者。使用 with
语句来管理此类资源。
关于python - 我应该关闭 makefile 的套接字并且它是独立的原始套接字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12203800/