我正在尝试创建一个上下文管理器,该管理器使用
但这将导致:
在下一次尝试中,我尝试显式创建一个上下文管理器类:
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')
#contextmanager
不是@contextmanager
。 - user2357112finally
子句中。 - user2357112print('in finally clause')
只是我在提问之前尝试其他事情时留下的(无害)残留物。 - martineau