Created
June 29, 2019 20:16
-
-
Save mildocjr/1711dedbd1ed499e132245dfdf8d2a53 to your computer and use it in GitHub Desktop.
multiprocessing3.py
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
| 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