0

I am getting started with multiprocessing and tried to do a simple code using Process, however the result is different from Colab and my local pc. It seem on my pc (using Windows and Jupyter lab via Anaconda) the process doesn't start. The code is:

from multiprocessing import Process
def teste(a):
    print(a)
if __name__ == '__main__':
    print('ok')
    p = Process(target=teste, args=(1,))
    p.start()
    p.join()
    print('b')
    print(p)

The result when running on Colab is:

"ok
1
b
Process name='Process-10' pid=3765 parent=493 stopped exitcode=0>"

However if I run on my PC is:

"ok
b
Process name='Process-10' pid=27844 parent=10844 stopped exitcode=0>"

It seems the comand p.start() is not working. What can it be?

Oalvinegro
  • 458
  • 5
  • 21
  • I don't know what's happening to the output from the print(a), but you're code is running. That's evident from the fact that `p.join()` succeeded and the `p` is print out as a stopped process with exit code 0. – Frank Yellin Jul 02 '23 at 13:19
  • on Windows, you generally need to use `from multiprocessing import freeze_support` then call `freeze_support()` when you start `__main__`. – Mike McKerns Jul 02 '23 at 23:01
  • @MikeMcKerns by doing this it works, but only if I export the file as an executable script an run on VS Code for instance..Is there a way to run it usnig Jupyter Notebook? – Oalvinegro Jul 03 '23 at 13:22
  • 2
    Getting mp to work on Jupyter is difficult on non-Linux systems. I'm not sure if it works on Linux either. Multiprocessing starts a new Python process, and that process needs a file to read. – Frank Yellin Jul 03 '23 at 14:06
  • 1
    [Here](https://stackoverflow.com/q/50937362/8508004) and [here](https://stackoverflow.com/q/47313732/8508004) may also help anyone coming across this post because of tags and mention of Jupyter. – Wayne Jul 03 '23 at 16:44
  • For Jupyter Lab you need to place function `teste` in a separate module and import it. – Booboo Jul 04 '23 at 11:45

0 Answers0