Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>if __name__ == '__main__'</code> is in the wrong place. A quick fix would be to protect only the call to mp_factorizer like Janne Karila suggested:</p> <pre><code>if __name__ == '__main__': print mp_factorizer(list, 2, someobject) </code></pre> <p>However, on windows the main file will be executed once on execution + once for every worker thread, in this case 2. So this would be a total of 3 executions of the main thread, excluding the protected part of the code.</p> <p>This can cause problems as soon as there are other computations being made in the same main thread, and at the very least unnecessarily slow down performance. Even though only the worker function should be executed several times, in windows everything will be executed thats not protected by <code>if __name__ == '__main__'</code>.</p> <p>So the solution would be to protect the whole main process by executing all code only after <code>if __name__ == '__main__'</code>.</p> <p>If the worker function is in the <em>same file</em>, however, it needs to be excluded from this if statement because otherwise it can not be called several times for multiprocessing.</p> <p>Pseudocode main thread:</p> <pre><code># Import stuff if __name__ == '__main__': #execute whatever you want, it will only be executed #as often as you intend it to #execute the function that starts multiprocessing, #in this case mp_factorizer() #there is no worker function code here, it's in another file. </code></pre> <p>Even though the whole main process is protected, the worker function can still be started, as long as it is in another file.</p> <p>Pseudocode main thread, with worker function:</p> <pre><code># Import stuff #If the worker code is in the main thread, exclude it from the if statement: def worker(): #worker code if __name__ == '__main__': #execute whatever you want, it will only be executed #as often as you intend it to #execute the function that starts multiprocessing, #in this case mp_factorizer() #All code outside of the if statement will be executed multiple times #depending on the # of assigned worker threads. </code></pre> <p>For a longer explanation with runnable code, see <a href="https://stackoverflow.com/questions/14583742/using-multiprocessing-in-a-sub-process">using multiprocessing in a sub process</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload