ymmx ymmx - 11 months ago 160
Python Question

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

I'm trying to launch a function in a new thread because the function makes something not related to the main program.

I tried to do this with multiprocessing module as:

import multiprocessing
import time

def mp_worker(a):
print('a:' +str(a))

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

But I have a bunch of error:

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
File "C:\Anaconda3\lib\multiprocessing\spawn.py", line 226, in prepare
File "C:\Anaconda3\lib\multiprocessing\spawn.py", line 278, in _fixup_main_from_path
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>
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
File "C:\Anaconda3\lib\multiprocessing\spawn.py", line 137, in _check_not_importing_main
is not going to be frozen to produce an executable.''')
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__':

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

Someone knows how I can launch whatever function I want in a new thread and be sure that the main program is still running normally? I'm a little lost with multiprocessing, I admit that :)
I aim to launch some displays (graphical or print I don't know yet) for the user in new threads without interrupting the main program.

Answer Source

What you are doing isn't multithreading, it's muliprocessing.




Python ยป 3.6.1 Documentation: threading.Thread
SO Q&A: multiprocessing-vs-threading-python

Beside this, you can overcome the Error adding a time.sleep(0.2)after start(...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download