-2
import multiprocessing
import time

def sq(a):
    for n in a:
        time.sleep(0.2)
        print('square '+str(n*n))

def cub(a):
    for n in a:
        time.sleep(0.2)
        print('cube '+str(n*n*n))

if __name__ == "__main__":
    arr=[2,3,4,5]
    p1=multiprocessing.Process(target=sq,args=(arr,))
    p2=multiprocessing.Process(target=cub,args=(arr,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

    print('done')

Here I'm getting output only done but not the square and cube.

This is the code what I'm trying for multi-processing. I got the output in case of multi-threading as most of syntax is same but not in case of multi-processing.

Nikhil
  • 16
  • 7
  • 3
    It's very unclear what's being asked here. Can you clarify? – Carcigenicate Oct 16 '18 at 12:16
  • 1
    Seems to work fine for me. What version of Python are you running? And what OS are you on? – John Anderson Oct 16 '18 at 12:20
  • OS- Windows 7, Python Version- 2.7 – Nikhil Oct 16 '18 at 12:21
  • When I had problems with multiprocessing in Python, it was caused by my IDE, and that I was using iPython. Try running this with the command line with "python [yourFileName].py". I copy and pasted your code and it worked fine for me when doing this... – Namyts Oct 16 '18 at 12:37

1 Answers1

0

Unfortunately the multiprocessing library in Python will not work in an IDE. I just double checked with 2 IDEs in 2 different environments but none of them accesses the methods. When I ran it from the command line it worked fine. There is an explanation in this post here Python multiprocessing

Edit:

From the video you shared in your comment, it appears that Pycharm is an exception. I tested in mine and it worked. The point is there is nothing wrong with the code. If it is not working in your IDE, it means it is not supported. So I guess you can either use Pycharm like in the tutorial or use command line.

Wazaki
  • 899
  • 1
  • 8
  • 22
  • Check here he is using IDE and output is shown in IDE https://www.youtube.com/watch?v=Lu5LrKh1Zno&list=PLeo1K3hjS3uub3PRhdoCTY8BxMKSW7RjN&index=2 – Nikhil Oct 16 '18 at 13:11