Created
October 10, 2019 06:44
-
-
Save scionoftech/f380f9b013fab405f62426f45933eaf8 to your computer and use it in GitHub Desktop.
A small python code to use multiprocessing package
This file contains hidden or 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 multiprocessing import Pool | |
# import time | |
def square(x): | |
# calculate the square of the value of x | |
# time.sleep(2) | |
return x*x | |
if __name__ == '__main__': | |
# Define the dataset | |
dataset = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] | |
# Output the dataset | |
print ('Dataset: ' + str(dataset)) | |
# Run this with a pool of 5 agents having a chunksize of 3 until finished | |
agents = 5 | |
chunksize = 3 | |
with Pool(processes=agents) as pool: | |
result = pool.map(square, dataset, chunksize) | |
# Output the result | |
print ('Result: ' + str(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment