Created
February 4, 2025 10:31
-
-
Save pinkwah/f746ccef4b08510fa62782f74a4b8567 to your computer and use it in GitHub Desktop.
Silly multiprocessing example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
from argparse import ArgumentParser, Namespace | |
from multiprocessing import Pool | |
import math | |
def is_prime(maybe_prime: int) -> bool: | |
for i in range(2, maybe_prime): | |
if (maybe_prime % i) == 0: | |
return False | |
return True | |
def parse_args() -> Namespace: | |
ap = ArgumentParser() | |
ap.add_argument("n", type=int) | |
ap.add_argument("-j", "--nproc", type=int, default=1) | |
return ap.parse_args() | |
def main() -> None: | |
args = parse_args() | |
with Pool(processes=args.nproc) as pool: | |
total = sum(pool.map(is_prime, range(2, args.n))) | |
print(f"There are {total} prime numbers less than {args.n}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment