Sobes.tech
Junior — Middle

When processing a large number of files, what will ensure higher performance: using multithreading or asynchronous operations for moving files into separate folders?

sobes.tech AI

Answer from AI

When moving a large number of files, the main operation is input/output (I/O), not computation. In Python, multithreading is limited by the GIL (Global Interpreter Lock), which does not allow effective use of multiple threads for CPU-intensive tasks, but for I/O tasks, multithreading can help.

However, asynchronous operations (asyncio) in Python are better suited for a large number of I/O operations, as they do not block the thread while waiting for the operation to complete, effectively switching between tasks.

For moving files (disk operations), asynchrony can provide better results if an asynchronous API for the filesystem is used (e.g., aiofiles). If the standard synchronous API is used, then multithreading might be simpler and more effective.

Summary:

  • If an asynchronous API for file operations exists — asynchronous is preferable.
  • If not — multithreading can help, but the gain is limited.

Example with aiofiles:

import asyncio
import aiofiles.os

async def move_file(src, dst):
    await aiofiles.os.rename(src, dst)

async def main(files):
    tasks = [move_file(src, dst) for src, dst in files]
    await asyncio.gather(*tasks)

# Run
# asyncio.run(main(list_of_files))