简体   繁体   English

导入和多处理,代码不等待进程完成

[英]Imports and Multiprocessing, code doesn't wait for process to finish

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.代码没有睡 5 秒,而是完成了。 If I comment out the import it sleeps.如果我注释掉导入,它就会休眠。

The x contains functions a, b, c, imports from math and a global variable. x 包含函数 a、b、c、从数学导入和全局变量。 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.我绝对没有在 x 中运行任何代码。

Seems that spyder doesn't handle multiprocessing well.似乎 spyder 不能很好地处理多处理。 I didn't think it was important, I was trying to run the code inside spyders iPython console.我认为这并不重要,我试图在 spyders iPython 控制台中运行代码。 More info:更多信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM