如何在新线程中启动函数而不在python中锁定主程序?

时间:2017-04-12 13:22:22

标签: python multithreading thread-safety python-multithreading

我正在尝试在新线程中启动一个函数,因为该函数会生成与主程序无关的内容。

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

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.

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

2 个答案:

答案 0 :(得分:1)

您所做的不是multithreading,而是muliprocessing 更改。

multiprocessing.Process(...

用。

threading.Thread(...   

Python»3.6.1文档threading.Thread
SO Q&amp; A:multiprocessing-vs-threading-python

除此之外,您还可以克服在time.sleep(0.2)

之后添加start(...时出错的问题

答案 1 :(得分:0)

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

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

因此,您的问题的答案是您必须在其他地方查找多处理问题的原因,而不是在问题中列出的代码部分。

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

在评论中对其中一个答案所说的是:

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** :(