0

I'm trying to do some multiprocessing with Python with some example code and I can get it to work in Python, but not in Spyder with IPython.

I have a python file: test.py

It contains this example code:

import multiprocessing

def worker():
    """worker function"""
    print( 'Worker')
    return

if __name__ == '__main__':
    print('run this code')
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker)
        jobs.append(p)
        p.start()

I am using the Anaconda3 distribution of Python. If I go to the Anaconda prompt and type:

python test.py

The code works as expected.

And, if I try the same thing with the IPython:

IPython test.py

Again, the code works!!

However, I like to do my development in the Spyder IDE that comes with Anaconda3. And in Spyder, the console is IPython.

If I run test.py in Spyder with the IPython console, I get run this code printing to the console, but the multiprocessing function is not executed and there are no errors reported.

Any idea what's going on here?

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
Flux Capacitor
  • 1,215
  • 5
  • 24
  • 40
  • How are you running this in Spyder? This doesn't really have anything to do with Anaconda. – juanpa.arrivillaga Oct 26 '18 at 16:48
  • @juanpa.arrivillaga I'm just running the file... (i.e. press F5) – Flux Capacitor Oct 26 '18 at 16:55
  • So, I'm pretty sure that copies the text and runs it in the REPL. Can you please clarify? Anyway, how are you sure the multiprocessing code isn't running? The stdout may just not be playing well with the IPython REPL (certainly wouldn't be the first time). – juanpa.arrivillaga Oct 26 '18 at 16:57
  • @juanpa.arrivillaga Interesting. It turns out you are right! Do you know why the results from the worker function aren't printing to the screen? Or better yet, do you know how I can fix this to get the results to print to the screen? – Flux Capacitor Oct 26 '18 at 17:08
  • Because you are running it in a REPL, an IPython REPL which does funky things with stdin/stdout, and especially when you involve different processes, a lot of things could be not working as you'd hope. And then who knows what's going on with the Spyder terminal emulator. REPLs aren't really for running production code, it's for exploration/debugging. You can just run it from your Spyder terminal like you would a regular terminal, no? – juanpa.arrivillaga Oct 26 '18 at 18:07
  • @juanpa.arrivillaga Yeah, I don't really know what REPL is... I had to google it. What I do know, is that when I used a WinPython distribution previously, I had the option to run code in a regular python terminal or an IPython console in Spyder. Under the Anaconda3 distribution, there is only the IPython console in Spyder. Perhaps I should just move back to what worked for me... A shame though, because Anaconda comes packaged with a lot of good stuff. – Flux Capacitor Oct 26 '18 at 18:37
  • Pretty sure Spyder let's you run a regular Python repl – juanpa.arrivillaga Oct 26 '18 at 19:05
  • @juanpa.arrivillaga, the Python console was removed in our 3.2 version, released more than a year ago. – Carlos Cordoba Oct 28 '18 at 01:27
  • @FluxCapacitor, you failed to mention one critical thing: I'm sure you're on Windows because your code works fine on Linux and macOS. – Carlos Cordoba Oct 28 '18 at 01:28

1 Answers1

2

(Spyder maintainer here) This question has been asked several times here and the first Google result of Spyder multiprocessing gives you the right answer:

Simple Python Multiprocessing function in Spyder doesn't output results

This is another question with an answer of my own:

No multiprocessing print outputs (Spyder)

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124