Last active
May 8, 2020 23:25
-
-
Save thieu1995/bde85633952ac92da331b9344c08a38f to your computer and use it in GitHub Desktop.
How to handle multiprocessing in python
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
### Thieu Nguyen (https://www.researchgate.net/profile/Thieu_Nguyen6) | |
### Excellent article here: https://medium.com/@urban_institute/using-multiprocessing-to-make-python-code-faster-23ea5ef996ba | |
import time | |
import multiprocessing | |
def basic_func(x): | |
if x == 0: | |
return 'zero' | |
elif x % 2 == 0: | |
return 'even' | |
else: | |
return 'odd' | |
def multiprocessing_func(x): | |
y = x * x | |
time.sleep(10) | |
print('{} squared results in a/an {} number'.format(x, basic_func(y))) | |
if __name__ == '__main__': | |
starttime = time.time() | |
pool = multiprocessing.Pool() | |
pool.map(multiprocessing_func, range(0, 10)) | |
pool.close() | |
print('That took {} seconds'.format(time.time() - starttime)) |
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
### Thieu Nguyen (https://www.researchgate.net/profile/Thieu_Nguyen6) | |
### Excellent article here: https://medium.com/@urban_institute/using-multiprocessing-to-make-python-code-faster-23ea5ef996ba | |
import time | |
import multiprocessing | |
def basic_func(x): | |
if x == 0: | |
return 'zero' | |
elif x % 2 == 0: | |
return 'even' | |
else: | |
return 'odd' | |
def multiprocessing_func(x): | |
y = x * x | |
time.sleep(10) | |
print('{} squared results in a/an {} number'.format(x, basic_func(y))) | |
if __name__ == '__main__': | |
starttime = time.time() | |
processes = [] | |
for i in range(0, 10): | |
p = multiprocessing.Process(target=multiprocessing_func, args=(i,)) | |
processes.append(p) | |
p.start() | |
for process in processes: | |
process.join() | |
print('That took {} seconds'.format(time.time() - starttime)) |
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
### Thieu Nguyen (https://www.researchgate.net/profile/Thieu_Nguyen6) | |
### Excellent article here: https://medium.com/@urban_institute/using-multiprocessing-to-make-python-code-faster-23ea5ef996ba | |
import time | |
def basic_func(x): | |
if x == 0: | |
return 'zero' | |
elif x % 2 == 0: | |
return 'even' | |
else: | |
return 'odd' | |
starttime = time.time() | |
for i in range(0, 10): | |
y = i * i | |
time.sleep(10) | |
print('{} squared results in a/an {} number'.format(i, basic_func(y))) | |
print('That took {} seconds'.format(time.time() - starttime)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment