为什么上下文管理器没有关闭文件描述符?

4
我正在尝试创建一个上下文管理器,该管理器使用mmap,而mmap本身也是一个上下文管理器。最初我遇到了一个愚蠢的打开文件问题(为什么mmap没有关闭关联文件而出现PermissionError: [WinError 32]?),并且很快就有一个答案解释了为什么它不能按照预期工作。

基于这些信息,我尝试了两种不同的方法来解决这个问题,但都没有成功。

第一种方法是使用contextlib@contextmanager装饰器:

from contextlib import contextmanager
import os
import mmap

#contextmanager
def memory_map(filename, access=mmap.ACCESS_WRITE):
    size = os.path.getsize(filename)
    fd = os.open(filename, os.O_RDWR)
    print('about to yield')
    with mmap.mmap(fd, size, access=access) as m:
        yield m
    print('in finally clause')
    os.close(fd)  # Close the associated file descriptor.

test_filename = 'data'

# First create the test file.
size = 1000000
with open(test_filename, 'wb') as f:
     f.seek(size - 1)
     f.write(b'\x00')

# Read and modify mmapped file in-place.
with memory_map(test_filename) as m:  # Causes AttributeError: __enter__
    print(len(m))
    print(m[0:10])
    # Reassign a slice.
    m[0:11] = b'Hello World'

# Verify that changes were made
print('reading back')
with open(test_filename, 'rb') as f:
     print(f.read(11))

# Delete test file.
# Causes:
# PermissionError: [WinError 32] The process cannot access the file because it
# is being used by another process: 'data'
os.remove(test_filename)

但这将导致:
Traceback (most recent call last):
  File "memory_map.py", line 27, in <module>
    with memory_map(test_filename) as m:  # Causes AttributeError: __enter__
AttributeError: __enter__

在下一次尝试中,我尝试显式创建一个上下文管理器类:
import os
import mmap

class MemoryMap:
    def __init__(self, filename, access=mmap.ACCESS_WRITE):
        print('in MemoryMap.__init__')
        size = os.path.getsize(filename)
        self.fd = os.open(filename, os.O_RDWR)
        self.mmap = mmap.mmap(self.fd, size, access=access)

    def __enter__(self):
        print('in MemoryMap.__enter__')
        return self.mmap

    def __exit__(self, exc_type, exc_value, traceback):
        print('in MemoryMap.__exit__')
        os.close(self.fd)  # Close the associated file descriptor.
        print('  file descriptor closed')


test_filename = 'data'

# First create the test file.
size = 1000000
with open(test_filename, 'wb') as f:
     f.seek(size - 1)
     f.write(b'\x00')

# Read and modify mmapped file in-place.
with MemoryMap(test_filename) as m:
    print(len(m))
    print(m[0:10])
    # Reassign a slice.
    m[0:11] = b'Hello World'

# Verify that changes were made
print('reading back')
with open(test_filename, 'rb') as f:
     print(f.read(11))

# Delete test file.
# Causes PermissionError: [WinError 32] The process cannot access the file
# because it is being used by another process: 'data'
os.remove(test_filename)

这使得它更进一步,但是PermissionError又回来了——这真的让我很困惑,因为你可以在输出中看到文件描述符已经被关闭:

in MemoryMap.__init__
in MemoryMap.__enter__
1000000
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
in MemoryMap.__exit__
  file descriptor closed
reading back
b'Hello World'
Traceback (most recent call last):
  File "memory_map2.py", line 47, in <module>
    os.remove(test_filename)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'data'

看起来我又卡住了。你有什么想法是出了什么问题(以及如何解决)?此外,如果两个都能修复,你有意见哪一个更好吗?

解决方案

这两个片段都有错误。第一个是一个简单的排版错误。上下文管理器装饰器被注释掉了。应该是:

@contextmanager  # Leading "#" changed to "@".
def memory_map(filename, access=mmap.ACCESS_WRITE):
    size = os.path.getsize(filename)
    fd = os.open(filename, os.O_RDWR)
    ...

第二个问题是由于mmap本身在__exit__()方法中没有被关闭,只有相关的文件描述符被关闭。这个问题我从来没有想到过,因为它抛出的异常与第一个问题相同。

    def __exit__(self, exc_type, exc_value, traceback):
        print('in MemoryMap.__exit__')
        self.mmap.close()  # ADDED.
        os.close(self.fd)  # Close the associated file descriptor.
        print('  file descriptor closed')

这个能帮到你吗?我只看了错误信息。https://dev59.com/OF4d5IYBdhLWcg3wDvG1 - Elis Byberi
2
#contextmanager 不是 @contextmanager - user2357112
另外,你的“在 finally 子句中”实际上并不在 finally 子句中。 - user2357112
@ElisByberi:谢谢。我实际上看了这个问题,虽然它很相似,但没有看出它如何适用——但根据下面的答案,也许不是... - martineau
@user2357112:是的,第一个片段就有问题。复制粘贴自我的上一个问题时可能出错了。 - martineau
@user2357112:另外,print('in finally clause')只是我在提问之前尝试其他事情时留下的(无害)残留物。 - martineau
1个回答

3

如果您进行第二次尝试,您需要关闭内存映射文件:

def __exit__(self, exc_type, exc_value, traceback):
    self.mm.close()
    print('in MemoryMap.__exit__')
    os.close(self.fd)  # Close the associated file descriptor.
    print('  file descriptor closed')

我会接受这个答案,因为你将其发布为正式答案...谢谢。它和我的问题下的评论为两个版本都提供了修复方法。我猜我一直混淆了mmap对象的关闭和与之关联的文件的关闭。 - martineau
1
在Windows上,一个文件不能被多个进程访问。您需要在文件对象之前关闭mmap对象。这个答案是正确的。 - Elis Byberi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接