check_async.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Support for checking code asynchronously."""
  2. import logging
  3. from concurrent.futures import ProcessPoolExecutor
  4. from pathlib import Path
  5. from typing import List
  6. from pylama.config import Namespace
  7. from pylama.errors import Error
  8. try:
  9. import multiprocessing
  10. CPU_COUNT = multiprocessing.cpu_count()
  11. except (ImportError, NotImplementedError):
  12. CPU_COUNT = 1
  13. from pylama.core import run
  14. LOGGER = logging.getLogger("pylama")
  15. def worker(params):
  16. """Do work."""
  17. path, code, options, rootdir = params
  18. return run(path, code=code, rootdir=rootdir, options=options)
  19. def check_async(
  20. paths: List[str], code: str = None, options: Namespace = None, rootdir: Path = None
  21. ) -> List[Error]:
  22. """Check given paths asynchronously."""
  23. with ProcessPoolExecutor(CPU_COUNT) as pool:
  24. return [
  25. err
  26. for res in pool.map(
  27. worker, [(path, code, options, rootdir) for path in paths]
  28. )
  29. for err in res
  30. ]
  31. # pylama:ignore=W0212,D210,F0001