Async programming in python

I am not getting “hello” in different places as told in video.

Hey @Bhawna, have you tried running it several times ? It is possible it gives you hello at the same place 10 times and then change it to some other place . So please run it multiple times ! Also this is not an issue even if it gives at the same place. I would recommend you to continue with the further contents of the course !

If you feel there’s any doubt, you can still ask me again anytime :+1:
I hope this helps !
Happy Learning :slight_smile:


I am also not getting 4 as output in cell containing proc.start()

Hey @Bhawna, you can try replacing this line of yours process.append(Process(target=showSquare())) by this line :

process.append(Process(target=showSquare))

Also as I can see there are around 30 processes running concurrently on your OS, I would request you to restart your kernel and then run all the cells !

I hope this resolves your doubt :+1:
Please mark the doubts as resolved in your doubts section :slight_smile:
Happy Learning !


After removing parenthesis ,I am not getting any output.

Hey @Bhawna, I am running the following code in the same sequence as you are :

from multiprocessing import Process
def showsquare(num=2):
    print(num ** 2)

procs=[]
for i in range(5):
    procs.append(Process(target=showsquare))

for proc in procs :
    proc.start()
print("Hello")

And I do get the output as :

4
4
4
4
Hello
4

So I would suggest you to try running the above code in the same sequence as I have written. Even if does not give you output in the first time, when you rerun it from the beginning ,it would give you the output !

You must have noticed that the same thing happens with sir in the video. But after re running the code, sir do get the output. You can watch the video once again and observe this yourself :slight_smile:

I hope this helps you resolve your doubt !
Happy Learning :slight_smile:

Yes,

It is working when I put whole code in single cell ,but hello is always printed in last .

Yes that is not an issue. If you particularily want hello to get printed at some other position than 5, then restart the kernel and run atleast 10-20 times. Only then you would be able to see hello at some different place. But this is not an error and you can simply move to further contents of the course :slight_smile:

I hope this resolves your doubt !
Happy Learning :slight_smile:

Ok Sir,but If I am writting (target=showsquare) without parenthesis ,I am not getting any output of showsquare function but Sir’s was working without giving parenthesis after showsquare function.
And also writting args was not working for me instead when I am giving values in parenthesis like this:-
showsquare(5) ,then it is working .Help!!

Yes if you use showSquare with parenthesis, then you would get output like this :

Screenshot 2020-07-17 at 3.46.16 PM

For the args doubt, try running this in your notebook, this should work :

from multiprocessing import Process
def showsquare(num=2):
    print(num ** 2)

procs=[]
for i in range(5):
    procs.append(Process(target=showsquare,args=(i+1,)))

for proc in procs :
    proc.start()
print("Hello")

This should give you the output !
I hope this helps !
Happy Learning :+1:

args is not working

Ohh what version of python are you using ? Maybe that’s the issue. Try to restart the kernel and run again…maybe it works…I am running exactly the same code and it do give the required output !

Happy Learning :slight_smile:

python 3.7.4 is the version,I have restarted kernel and ran many times but no output.

Can you try running the code without giving the default argument to the function ? Your function should be something like this :

def showsquare(num):
    print(num ** 2)

Let me know the output !

There is no output here

Pease share your code with me.

def showsquare(num):
print(num ** 2) is the code

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Ok, I’ll ask the mentors to solve this as the code is exactly the same at my end but still you are not getting the output. Please wait till our team contacts you. Thank you for your patience !

You can continue with the further contents of the course till then ! :+1:
Happy Learning :slightly_smiling_face: