Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use multiprocessing in a module with windows?

I'm currently going through some pre-existing code with the goal of speeding it up. There's a few places that are extremely good candidates for parallelization. Since Python has the GIL, I thought I'd use the multiprocess module.

However from my understanding the only way this will work on windows is if I call the function that needs multiple processes from the highest-level script with the if __name__=='__main__' safeguard. However, this particular program was meant to be distributed and imported as a module, so it'd be kind of clunky to have the user copy and paste that safeguard and is something I'd really like to avoid doing.

Am I out of luck or misunderstanding something as far as multiprocessing goes? Or is there any other way to do it with Windows?

like image 200
Tyler Avatar asked Oct 31 '22 02:10

Tyler


2 Answers

For everyone still searching:

inside module

from multiprocessing import Process
    
def printing(a):
    print(a)

def foo(name):
    var={"process":{}}
    if name == "__main__":
        for i in range(10):
            var["process"][i] = Process(target=printing , args=(str(i)))
            var["process"][i].start()

        for i in range(10):
            var["process"][i].join

inside main.py

import data

name = __name__

data.foo(name)

output:

>>2
>>6
>>0
>>4
>>8
>>3
>>1
>>9
>>5
>>7

I am a complete noob so please don't judge the coding OR presentation but at least it works.

like image 72
Julian Avatar answered Nov 15 '22 06:11

Julian


As explained in comments, perhaps you could do something like

#client_main.py
from mylib.mpSentinel import MPSentinel

#client logic

if __name__ == "__main__":
    MPSentinel.As_master()

#mpsentinel.py

class MPSentinel(object):

    _is_master = False

@classmethod
def As_master(cls):
    cls._is_master = True

@classmethod
def Is_master(cls):
    return cls._is_master

It's not ideal in that it's effectively a singleton/global but it would work around window's lack of fork. Still you could use MPSentinel.Is_master() to use multiprocessing optionally and it should prevent Windows from process bombing.

like image 45
David Avatar answered Nov 15 '22 07:11

David