Python - Performing Tasks Concurrently

When using Python to perform tasks that involve showing and parsing of network configuration files, and other network device information via terminal connections such as Telnet and SSH, such tasks can often be time consuming if executed in serially, especially when connecting to many remote devices.

To execute such tasks faster, tasks can be performed concurrently. There are a few approaches you can take to achieve this, and each approach is appropriate for different applications:

  • Multithreading: This approach allows you to run multiple threads in parallel. This is useful for I/O-bound tasks like network requests.
  • Multiprocessing: This approach runs multiple processes in parallel, each with its own Python interpreter. It’s suitable for CPU-bound tasks.
  • Asyncio: This approach is useful for writing single-threaded concurrent code using async/await syntax. It’s particularly useful for I/O-bound and high-level structured network code.

For network I/O, using either multithreading or asyncio would be appropriate.