How to get Python multiprocessing module working on windows? I am trying to run a script within Blender that utilizes multiprocessing module.
According to documentation it is possible to use it ( see note at the end of "Strange errors using ‘threading’ module" ).


  Pythons threads only allow co-currency and won’t speed up your scripts on multi-processor systems, the subprocess and multiprocess modules can be used with Blender and make use of multiple CPU’s too..


So I tried running below script within Blender:

from multiprocessing import Pool

def test( v ):
    print(v)
    return v

p = Pool(2)

print( p.map(test, range(5) ) )


This however spawns as many new Blender instances (with separate windows) as there are workers in p (in my case 2).
Also it spews in System Console :

Can't read file: "C:\Program Files\Blender Foundation\Blender\-c", Unable to open the file.
Can't read file: "C:\Program Files\Blender Foundation\Blender\from multiprocessing.forking import main; main()", Unable to open the file.
Can't read file: "C:\Program Files\Blender Foundation\Blender\--multiprocessing-fork", Unable to open the file.
Can't read file: "C:\Program Files\Blender Foundation\Blender\2888", Unable to open the file.


I also thought about using os.fork() to do the same thing, but sadly Windows does not support that.

I've been looking all over Internet, but so far I was not able to find even one example proving that documentation is correct.

The only thing I found was thread on blenderartists.org, but it is locked and with no responses.



Update: This is a windows spesific limitation in the multiprocessing module. ~ideasman42.
Edit, this workaround is no longer needed as of 2.91.

This looks like a kind of ms-windows specific problem... at least a conflict with having an embedded Python interpreter and the environment which multiprocessing expects.
(its not exactly a bug, in that its not like Blender devs can fix some mistake to get this working).
Currently multiprocessing makes the assumption that its running in python and not running inside an application.

I managed to get multi-processing working on ms-windows, doing some workarounds.

sys.executable needs to point to Python executable.
The scripts __file__ needs to point to a file on-disk(not always the case - when executing a text block for example).

Heres an example of a workaround:
import sys
# are we running inside Blender?
bpy = sys.modules.get(&quot;bpy&quot;)
if bpy is not None:
    sys.executable = bpy.app.binary_path_python
    # get the text-block's filepath
    __file__ = bpy.data.texts[__file__[1:]].filepath
del bpy, sys

# end workaround!
# --------------------------------------------------

# Example from
# https://docs.python.org/3/library/multiprocessing.html
from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    if hasattr(os, 'getppid'):  # only available on Unix
        print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

def main():
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()


if __name__ == '__main__':
    main()