Skip to content

Instantly share code, notes, and snippets.

@scionoftech
Created October 10, 2019 06:44
Show Gist options
  • Save scionoftech/f380f9b013fab405f62426f45933eaf8 to your computer and use it in GitHub Desktop.
Save scionoftech/f380f9b013fab405f62426f45933eaf8 to your computer and use it in GitHub Desktop.
A small python code to use multiprocessing package
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