0

I post this small reproducible example of the issue I'm trying to solve. I'm using the multiproccessing python module and I want to print a string on the console, but nothing appears. If you execute the function without multiprocessing is working though.

import multiprocessing as mp

def print_works(a):
    print(f"it works {a}")
if __name__ == "__main__":
    l=[1,2,3]
    p = mp.Pool(processes=1)
    p.map(print_works, l)
    p.close()

The result I want to obtain is:

it works 1
it works 2
it works 3

If you execute the function in a for loop it works:

l=[1,2,3]
for i in l:
    print_works(i)

The result is the one I want to obtain:

it works 1
it works 2
it works 3

Note: This is a silly example of the functionality I want to implement. I'm using Python 3.6.8, Windows 10 and VS Code editor.

Ipa
  • 109
  • 1
  • 8
  • 1
    Your code works as expected for me. Python 3.8.3, Arch Linux. Note however, that the order of the prints is not defined. – attalos Jun 04 '20 at 13:20
  • Thanks for your fast response @attalos. The order doesn't matter in this case. It's curious, if I execute it on the terminal is working. It seems only failing on the IPython console of VS Code. Do you know what could be happening? – Ipa Jun 04 '20 at 13:29
  • Take it look at this: https://stackoverflow.com/questions/29630217/multiprocessing-in-ipython-console-on-windows-machine-if-name-requirement – attalos Jun 04 '20 at 13:37
  • 1
    Do you have to use the IPython console in VS Code? It seems a bit unreasonable for a parallel application. When I execute it normally (python3 file.py), also in VS Code, it works fine. – attalos Jun 04 '20 at 13:39
  • You're right I can use he console. I was just wondering why is not working in the IPython console. – Ipa Jun 04 '20 at 14:44

0 Answers0