Created
November 4, 2020 04:54
-
-
Save rozeappletree/121bbf147da0be9eeb2f77b22b4b8c38 to your computer and use it in GitHub Desktop.
multiprocessing in python
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
# importing the multiprocessing module | |
import multiprocessing | |
def print_cube(num): | |
""" | |
function to print cube of given num | |
""" | |
while True: | |
print("Cube: {}".format(num * num * num)) | |
def print_square(num): | |
""" | |
function to print square of given num | |
""" | |
while True: | |
print("Square: {}".format(num * num)) | |
if __name__ == "__main__": | |
# creating processes | |
p1 = multiprocessing.Process(target=print_square, args=(10, )) | |
p2 = multiprocessing.Process(target=print_cube, args=(10, )) | |
# starting process 1 | |
p1.start() | |
# starting process 2 | |
p2.start() | |
# wait until process 1 is finished | |
p1.join() | |
# wait until process 2 is finished | |
p2.join() | |
# both processes finished | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment