0

I am trying to start a process using the multiprocessing.Process example from the python documentation.

Here is the example code:

from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

I would expect the console to show me the output of the function f('bob'), but I only get to see the output of info('mainline').

So I think the process doesn't even start??

I have never before worked with multiprocessing, I bet it's a silly mistake I'm making.

I have also tried to set the start method multiprocessing.set_start_method('spawn') (see here), as 'spawn' seems to be the only valid one for windows.

But I only get a

RuntimeError: context has already been set

At the moment I think, I can't get the process to start.

Any Ideas how to solve this?

P.S. I am working on windows 10 in spyder 4.2.5 (maybe this something with the ipython console? Because I have heared, this is no normal python console).

But I have also tried the same example in the normal python shell, and it also only showed the output of info('mainline').

Andre
  • 321
  • 1
  • 12
  • 1
    Not really an answer to your question, but if using multiprocessing it's probably worth just using a standard console to run Python. Years ago I'd use IDLE and I think that didn't particularly like threads/processes. – Peter Oct 07 '21 at 12:55
  • Possible duplicate https://stackoverflow.com/questions/1196074/how-to-start-a-background-process-in-python – abdou_dev Oct 07 '21 at 13:19
  • Your code worked for me on Windows10 with the standard CPython interpreter (v 3.8.9). – sj95126 Oct 07 '21 at 14:44
  • @Peter: yes, you are right, when I run the script from cmd, it works. So I guess it is really something with spyder/ipython. Thanks for the hint! :) – Andre Oct 14 '21 at 08:02

1 Answers1

0

SOLVED: by running the script from cmd

Andre
  • 321
  • 1
  • 12