Skip to content

Instantly share code, notes, and snippets.

@mildocjr
Created June 29, 2019 20:16
Show Gist options
  • Select an option

  • Save mildocjr/1711dedbd1ed499e132245dfdf8d2a53 to your computer and use it in GitHub Desktop.

Select an option

Save mildocjr/1711dedbd1ed499e132245dfdf8d2a53 to your computer and use it in GitHub Desktop.
multiprocessing3.py
import multiprocessing
import time
numbers = range(100000, 1000000, 10000)
lock = multiprocessing.Lock()
def task(number):
global lock
print(f"Executing new Task with process {multiprocessing.current_process().pid}")
result = 0
for i in range(number):
result = result + i
lock.acquire()
try:
print(f"Result: {result}")
print(f"Task Executed with process {multiprocessing.current_process().pid}")
finally:
lock.release()
def main():
executor = multiprocessing.Pool(8)
executor.map(task, numbers)
executor.close()
if __name__ == "__main__":
start_time = time.time()
main()
print("\n\nCOMPLETE\n--- Took %s seconds ---" % (time.time() - start_time))
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment