Advertisement
cooperlees

Untitled

Feb 3rd, 2022
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. cooper@home1:/data/psx$ cat extract.py
  2. #!/usr/bin/env python3
  3.  
  4. import asyncio
  5. import multiprocessing
  6. from pathlib import Path
  7.  
  8.  
  9. async def extract_file(q):
  10. f = await q.get()
  11. print(f"-> Extracting {f}")
  12. proc = await asyncio.create_subprocess_shell(
  13. f"7z e '{f}' && rm -f '{f}'",
  14. stdout=asyncio.subprocess.PIPE,
  15. stderr=asyncio.subprocess.PIPE)
  16. _stdout, _stderr = await proc.communicate()
  17. if proc.returncode == 0:
  18. print(f"--> Finished {f}")
  19. else:
  20. print(f"!!> FAILED {f}")
  21.  
  22.  
  23. async def main():
  24. PSX_PATH = Path(__file__).parent
  25. all_files = PSX_PATH.glob("*.7z")
  26. q = asyncio.Queue()
  27.  
  28. for f in PSX_PATH.glob("*.7z"):
  29. await q.put(f)
  30.  
  31. coros = []
  32. for i in range(int(multiprocessing.cpu_count()/2)):
  33. coros.append(extract_file(q))
  34.  
  35. await asyncio.gather(*coros)
  36.  
  37.  
  38. asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement