Last active
February 27, 2016 16:54
-
-
Save drgarcia1986/b70c895b4d9b825f367f to your computer and use it in GitHub Desktop.
MultiProcess em Python e o drible no GIL (http://www.diego-garcia.info/2016/02/27/multi-process-em-python-e-fuga-do-gil/)
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
from concurrent.futures import ProcessPoolExecutor, as_completed | |
import sys | |
TO_CALCULATE = range(1000, 15000, 1000) | |
def primes_until(num): | |
result = [] | |
for p in range(2, num+1): | |
for i in range(2, p): | |
if p % i == 0: | |
break | |
else: | |
result.append(p) | |
return result | |
def run_serial(): | |
print({i: primes_until(i) for i in TO_CALCULATE}) | |
def run_multiprocess(): | |
waits = {} | |
with ProcessPoolExecutor() as executor: | |
waits = { | |
executor.submit(primes_until, i) : i | |
for i in TO_CALCULATE | |
} | |
print({ | |
waits[future]: future.result() | |
for future in as_completed(waits) | |
}) | |
if __name__ == '__main__': | |
""" | |
To run serial | |
$ python primes_numbers.py | |
To run multiprocess | |
$ python multi_requests.py multiprocess | |
""" | |
if len(sys.argv) > 1 and sys.argv[1] == 'multiprocess': | |
run_multiprocess() | |
else: | |
run_serial() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment