如何在 python 中不锁定主程序的情况下在新线程中启动函数?

How launch a function in a new thread without locking the main program in python?

我正在尝试在新线程中启动一个函数,因为该函数产生了一些与主程序无关的东西。

我尝试使用多处理模块执行此操作:

import multiprocessing
import time

def mp_worker(a):
    #time.sleep(a)
    print('a:' +str(a))
    return



for k in range(5):
    p = multiprocessing.Process(target= mp_worker , args=(k,))
    p.start()
    print('keep going :' + str(k))

但是我有一堆错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Anaconda3\lib\multiprocessing\spawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:\Anaconda3\lib\multiprocessing\spawn.py", line 115, in _main
prepare(preparation_data)
  File "C:\Anaconda3\lib\multiprocessing\spawn.py", line 226, in prepare
_fixup_main_from_path(data['init_main_from_path'])
  File "C:\Anaconda3\lib\multiprocessing\spawn.py", line 278, in _fixup_main_from_path
run_name="__mp_main__")
  File "C:\Anaconda3\lib\runpy.py", line 254, in run_path
pkg_name=pkg_name, script_name=fname)
  File "C:\Anaconda3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
  File "C:\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
  File "C:\Users\Maxime\PycharmProjects\NeuralMassModelSofware_Pyqt5\dj.py", line 14, in <module>
p.start()
  File "C:\Anaconda3\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)
  File "C:\Anaconda3\lib\multiprocessing\context.py", line 212, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Anaconda3\lib\multiprocessing\context.py", line 313, in _Popen
return Popen(process_obj)
  File "C:\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 34, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Anaconda3\lib\multiprocessing\spawn.py", line 144, in get_preparation_data
_check_not_importing_main()
  File "C:\Anaconda3\lib\multiprocessing\spawn.py", line 137, in _check_not_importing_main
is not going to be frozen to produce an executable.''')
RuntimeError: 
        An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

有人知道我如何在新线程中启动我想要的任何功能并确保主程序仍然运行正常吗?我对多处理有点迷茫,我承认 :) 我的目标是在不中断主程序的情况下在新线程中为用户启动一些显示(图形或打印我还不知道)。

这是我在 运行 您提供的代码时得到的:

python3.6 -u "multiprocessingError_Cg.py"
keep going :0
a:0
keep going :1
keep going :2
a:1
a:2
keep going :3
keep going :4
a:3
a:4
>Exit code: 0

所以你的问题的答案是你必须在别处寻找多处理问题的原因,而不是在你问题中列出的代码部分。

仔细查看答案和评论中提供的内容 - 也许它对您的情况也有帮助?

这里是对其中一个答案的评论中所说的话:

Dave: Would this work if beBusyFor (in your case mp_worker) uses multiprocessing internally?

@Dave just try it out and report back here if it worked. Haven't tested such a case yet, so my opinion that it should work leaving some mess of processes with no parents doesn't matter here and therefore shouldn't be taken in consideration.

Dave: **No cigar ending child processes unfortunately** :(

你所做的不是 multithreading,而是 muliprocessing
改变。

multiprocessing.Process(...

与。

threading.Thread(...   

Python » 3.6.1 文档threading.Thread
所以问答:multiprocessing-vs-threading-python

除此之外,您可以克服在 start(...

之后添加 time.sleep(0.2) 的错误