0

I'm trying to learn to use the multiprocessing module. Something rather strange is happening. The process I spawn doesn't finish, the script just finishes instantly.

from time import sleep
from x import a, b, c
import multiprocessing as mp
import sys

def s():
    sleep(5)

if __name__ == '__main__':
    p = mp.Process(target = s)
    p.start()
    p.join()


Instead of sleeping for 5s, the code just finishes. If I comment out the import it sleeps.

The x contains functions a, b, c, imports from math and a global variable. So looks something like this:

from math import sin,cos
g = 9.81
def a():
    ...
def b():
    ...
def c():
    ...

I haven't grasped the import logic properly, but I can't seem to figure out what is wrong, just makes no sense to me. I'm definitely not running any code in x.

  • I duplicated what you described here, though the functions `a()`, `b()` and `c()` just had a pass in them (which should not matter) and the sleep worked fine. It waited for 5 seconds before exiting. Not sure why you are seeing an immediate return. – Glenn Mackintosh May 20 '20 at 00:01
  • @GlennMackintosh my functions do some work and use sin and cos functions, the return statements are quite long though, but that shouldn't matter – dzonimaler13 May 20 '20 at 09:53
  • @GlennMackintosh seems like spyder doesn't handle multiprocessing that well, just googled it, didn't occur to me last night since it worked on some occasions. Think I'll be fine, thanks for trying to help – dzonimaler13 May 20 '20 at 10:00
  • If you think that the issue is spyder, then can I suggest that, after you try it somewhere else and prove that to be true, you come back and answer your own question so that others later can benefit from that information. – Glenn Mackintosh May 20 '20 at 14:11

1 Answers1

0

Seems that spyder doesn't handle multiprocessing well. I didn't think it was important, I was trying to run the code inside spyders iPython console. More info:

https://stackoverflow.com/a/48099756/11325544