I’ve been trying to run these two function parallelly using python. Early I tried using threads, but later learnt that multithreading is only for IO Bound Operations.Now, I’m trying using multiprocessing, still until unless the first function finishes it’s execution, the second doesn’t start. What is the reason for the same?
import multiprocessing,os,time
def printNumbers():
for i in range(100):
print('execution in thread no. 1 {}'.format(i))
print(os.getpid())
def printCharacters():
for i in range(20):
print('execution in second loop. in thread no. 2 {}'.format(i))
print(os.getpid())
if __name__=="__main__":
process1=multiprocessing.Process(target=printNumbers)
process2=multiprocessing.Process(target=printCharacters)
process1.start()
process2.start()
# process1.join()
# process2.join()
I tried using multiprocessing for the same, and was expecting it to be irregular like, first process printing few no. or one followed by second, followed by first and so on. But what happened was, that until unless the first process finished, the second didn’t start.
dracuusta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.