'ThreadPoolExecutor and main thread executing concurrently in Python

I am trying to understand Python's ThreadPoolExecutor a bit better and am trying to figure out if there is any difference in running all computation under a ThreadPoolExecutor vs running some in the main thread.

For example:

from concurrent import futures


def fn1():
  while True:
    ...


def _fn2():
  ...


def fn2():
  while True:
    _fn2()

Is there a difference between:

executor = futures.ThreadPoolExecutor()
for completed in futures.as_completed(
    [executor.submit(fn1), executor.submit(fn2)]):
  completed.result()

vs

executor = futures.ThreadPoolExecutor()
future_fn1 = executor.submit(fn1)

# Simulate fn in main thread, but with check on future_fn1.
while True:
  _fn2()
  if future_fn1.done():
    future_fn1.result()

What are the main differences between the two approaches? Should I expect a big performance difference - e.g. threads being more efficiently managed if the executor is used for both?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source