@@ -39,11 +39,15 @@ async def put(self, data):
3939 await self .q .put (ident )
4040 await self ._save_manifest ()
4141
42- async def get (self , handle_only = False ):
42+ async def get (self , handle_only = False , delete = True ):
4343 async with self ._manifest_lock :
44- msg = await self .q .get ()
45- await self ._save_manifest ()
46- return await self ._get_file (msg , handle_only = handle_only )
44+ while True :
45+ msg = await self .q .get ()
46+ await self ._save_manifest ()
47+ try :
48+ return await self ._get_file (msg , handle_only = handle_only , delete = delete )
49+ except FileNotFoundError :
50+ pass
4751
4852 async def _save_manifest (self ):
4953 await self ._loop .run_in_executor (pool , self ._write_manifest )
@@ -59,13 +63,22 @@ def _read_manifest(self):
5963 except FileNotFoundError :
6064 return []
6165
62- async def _get_file (self , path , handle_only = False ):
66+ async def _get_file (self , path , handle_only = False , delete = True ):
67+ """
68+ Retrieves a file from disk. If handle_only is True then we will
69+ return the handle to the file and do nothing else. Otherwise the file
70+ is read into memory all at once and returned. If delete is True (the
71+ default) and handle_only is False (the default) then the underlying
72+ file will be removed as well.
73+ """
6374 path = os .path .join (self ._message_path , path )
6475 fp = await self ._loop .run_in_executor (pool , open , path , "rb" )
6576 if handle_only :
6677 return fp
6778 bytes = await self ._loop .run_in_executor (pool , lambda : fp .read ())
6879 fp .close ()
80+ if delete :
81+ os .remove (path )
6982 return bytes
7083
7184 def _write_file (self , data , ident ):
0 commit comments