pythonadvanced

Threading Parallel

Advanced Python pattern: threading-parallel

python
from concurrent.futures import ThreadPoolExecutor
import time

def fetch(i: int) -> str:
    time.sleep(0.05)
    return f'item-{i}'

with ThreadPoolExecutor(max_workers=4) as ex:
    out = list(ex.map(fetch, range(8)))
print(out)

Use Cases

  • advanced programming
  • patterns

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.