ymmx
ymmx

Reputation: 4967

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):
    #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))

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
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.

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.

Upvotes: 0

Views: 334

Answers (2)

stovfl
stovfl

Reputation: 15533

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

multiprocessing.Process(...

with.

threading.Thread(...   

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(...

Upvotes: 1

user7711283
user7711283

Reputation:

This is what I am getting when running the code you have provided:

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

So the answer to your question is that you have to look for the cause of the trouble with multiprocessing elsewhere, but not in the section of code listed in your question.

Take a close look at what was provided in answers and comments here - maybe it could help also in your case?

Here what was said in comments to one of the answers there:

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

Upvotes: 0

Related Questions