不明白为什么多重处理会导致导入错误

时间:2018-10-27 23:19:58

标签: python python-3.x multiprocessing discord.py

我想做的是同时运行两个进程。一种是等待用户的输入,另一种是计时器,在达到时间限制后关闭第一个过程。在代码的初始部分运行后,我将调用以下函数。

def CaptchaStart(member):
    print(__name__)
    if __name__ == '__main__':
        print("SEQUENCE STARTED") #These are used to figure where we are
        p1 = Process(target=Question(member))
        p1.start()
        p2 = Process(target=Timer)
        p2.start()
        p1.join()
        p2.join()
    else:
        print("SEQUENCE FAILED") #These are used to figure where we are

我可能会犯很多错误,但是在显示if __name__ == '__main__':的地方我用__main__替换了cogs.captcha,这是文件正在运行的地方。在更改main之前,它将无法运行,而在更改main之后,它将出现以下错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\SoleF\AppData\Local\Programs\Python\Python35\lib\multiprocessing\spawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\SoleF\AppData\Local\Programs\Python\Python35\lib\multiprocessing\spawn.py", line 116, in _main
    self = pickle.load(from_parent)
  File "C:\Users\SoleF\Documents\SENTINEL\Red-DiscordBot\cogs\captcha.py", line 7, in <module>
    from __main__ import settings ImportError: cannot import name 'settings'

我对这种级别的Python还是陌生的,我对为什么出现这些错误感到困惑。

1 个答案:

答案 0 :(得分:-1)

Question(member)Timer是什么?

考虑到Process的目标必须是一个函数,并考虑不要调用它!只需将函数名称传递给目标

p1 = Process(target=Question, args=(member,))
相关问题