iOS thumbnails: add batch-thumbs command for future optimization

batch-thumbs pulls multiple files in one Python process session,
amortising the pymobiledevice3 startup cost. Not yet wired into the
app — thumbnails load lazily per-cell and cache permanently.

Known limitation: first load of iPhone thumbnails is slow (~2-3s each)
because each pull spawns a new Python process. After first view they
are instant from the on-disk cache.
This commit is contained in:
Kyle Bolen
2026-07-28 08:44:54 +00:00
parent 66faf4c7e5
commit b12cb4dd1e
2 changed files with 33 additions and 0 deletions

0
.attach_pid3511 Normal file
View File

View File

@@ -157,6 +157,37 @@ async def cmd_pull_thumb(device_path: str, local_path: str):
sys.exit(1) sys.exit(1)
async def cmd_batch_thumbs(pairs_json: str):
"""
Pull thumbnails for multiple files in a single AFC session.
Input: JSON array of [device_path, local_path] pairs.
Output: JSON array of {devicePath, ok} results.
"""
try:
pairs = json.loads(pairs_json)
lockdown = await get_lockdown()
afc = await get_afc(lockdown)
results = []
for device_path, local_path in pairs:
try:
os.makedirs(os.path.dirname(os.path.abspath(local_path)), exist_ok=True)
try:
data = await afc.fread(device_path, 512 * 1024, 0)
except Exception:
await afc.pull(device_path, local_path)
results.append({'devicePath': device_path, 'ok': True})
continue
with open(local_path, 'wb') as f:
f.write(data)
results.append({'devicePath': device_path, 'ok': True})
except Exception as e:
results.append({'devicePath': device_path, 'ok': False, 'error': str(e)})
print(json.dumps(results))
except Exception as e:
print(f'Error: {e}', file=sys.stderr)
sys.exit(1)
async def cmd_delete(device_path: str): async def cmd_delete(device_path: str):
try: try:
lockdown = await get_lockdown() lockdown = await get_lockdown()
@@ -183,6 +214,8 @@ if __name__ == '__main__':
asyncio.run(cmd_pull(sys.argv[2], sys.argv[3])) asyncio.run(cmd_pull(sys.argv[2], sys.argv[3]))
elif command == 'pull-thumb' and len(sys.argv) == 4: elif command == 'pull-thumb' and len(sys.argv) == 4:
asyncio.run(cmd_pull_thumb(sys.argv[2], sys.argv[3])) asyncio.run(cmd_pull_thumb(sys.argv[2], sys.argv[3]))
elif command == 'batch-thumbs' and len(sys.argv) == 3:
asyncio.run(cmd_batch_thumbs(sys.argv[2]))
elif command == 'delete' and len(sys.argv) == 3: elif command == 'delete' and len(sys.argv) == 3:
asyncio.run(cmd_delete(sys.argv[2])) asyncio.run(cmd_delete(sys.argv[2]))
else: else: