我试图处理大型数据结构,该数据结构存储的内存超出了ram的处理能力,因此不得不使用mmap。
这一切都按预期工作,但是在代码末尾,我想删除此临时文件,因为它不再使用。
我尝试使用rm删除文件或在文件运行时手动将其删除,但是我无权这样做。
这就是我的代码。
using Mmap
io = open("example.tmp", "w+")
v = Mmap.mmap(io,Vector{Int64},Int64(1e9))
close(io)
#Perform some actions on vector v
#attempt to delete here, rm("example.tmp") returns an EACCES error though
我希望能够在不再使用此.tmp文件时自动将其删除,我该怎么做?
答案 0 :(得分:1)
class Test001(APIView):
def __init__(self):
self.account_sid = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
self.auth_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
self.twilio_number = 'whatsapp:+14XXXXXXXX'
self.to_number = 'whatsapp:+14XXXXXXXXX'
def post(self, request):
client = Client(self.account_sid, self.auth_token)
new_message = client.messages.create(
from_=self.twilio_number,
body=message,
to=self.to_number
)
sid = new_message.sid
serializer = MessageSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
只要有对内存映射数据的引用,您可能会遇到权限问题。毕竟,您仍然可以使用using Mmap
io = open("example.tmp", "w+")
v = Mmap.mmap(io,Vector{Int64},Int64(1e9))
close(io)
#Perform some actions on vector v
v = nothing # delete reference to memory mapped data
GC.gc() # call garbage collector to be safe
rm("example.tmp") # should work now
。尝试通过设置v
并随后调用GC来删除/覆盖对数据的任何引用。