Skip to content

Instantly share code, notes, and snippets.

@soof-golan
Last active February 3, 2022 07:51
Show Gist options
  • Save soof-golan/a794308e7386855f50d59d5fc83396fe to your computer and use it in GitHub Desktop.
Save soof-golan/a794308e7386855f50d59d5fc83396fe to your computer and use it in GitHub Desktop.
Toy grep from Aur's parallelism dojo
import itertools
import os
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
pool = ThreadPoolExecutor(max_workers=os.cpu_count() * 2)
def find_needle(file, needle):
with open(file) as f:
yield from((file, lineno, line) for (lineno, line) in enumerate(f.readlines()) if needle in line)
# needle in a haystack
def grep(path: str, needle: str):
files = [name for name in sorted(Path(path).glob("*")) if name.is_file()]
tasks = [pool.submit(find_needle, file, needle) for file in files]
results = itertools.chain.from_iterable([fut.result() for fut in tasks])
for result in results:
file, lineno, line = result
print(f'{file}:{lineno}: {line}', end='')
if __name__ == '__main__':
grep(os.getcwd(), 'hay')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment