Skip to content

Instantly share code, notes, and snippets.

@filevich
Created May 20, 2020 05:16
Show Gist options
  • Save filevich/cf049872bf871d12d634fee901ba92ce to your computer and use it in GitHub Desktop.
Save filevich/cf049872bf871d12d634fee901ba92ce to your computer and use it in GitHub Desktop.
python pools example
# https://www.geeksforgeeks.org/python-map-function/
import time
def foo(bar):
time.sleep(2)
return 'foo ' + str(bar)
from multiprocessing.pool import ThreadPool
pool = ThreadPool(processes=4)
async_results = []
for i in range(4):
async_result = pool.apply_async(foo, (i,)) # tuple of args for foo
async_results.append(async_result)
# do some other stuff in the main process
pool.close()
pool.join()
print('other stuff')
# for async_result in async_results:
# print(async_result.get())
res = list(map(lambda asyncRes: asyncRes.get(), async_results))
print(res)
print(list(map((lambda x: x + 1), [1,2,3])))
# from multiprocessing import Pool
# foo = {1: []}
# def f(x):
# time.sleep(1)
# return x
# def main():
# pool = Pool()
# foo[1] = pool.map(f, range(10))
# pool.close()
# pool.join()
# print('sapee')
# print (foo)
# if __name__ == '__main__':
# main()
# # Python program to illustrate
# # the concept of race condition
# # in multiprocessing
# import multiprocessing
# # function to withdraw from account
# def withdraw(balance):
# for _ in range(10):
# balance.value = balance.value - 1
# # function to deposit to account
# def deposit(balance):
# for _ in range(10):
# balance.value = balance.value + 1
# # initial balance (in shared memory)
# balance = multiprocessing.Value('i', 100)
# # creating new processes
# p1 = multiprocessing.Process(target=withdraw, args=(balance,))
# p2 = multiprocessing.Process(target=deposit, args=(balance,))
# # starting processes
# p1.start()
# p2.start()
# # wait until processes are finished
# p1.join()
# p2.join()
# # print final balance
# print("Final balance = {}".format(balance.value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment