简体   繁体   中英

How to start 2 (or more) processes in parallel from a function

im trying to start 2 processes in parallel from a function in a class, but it is not working. I am trying something like that:

 from multiprocessing import Process

 class MyClass():

     #something here to create a large list L

     def myClassFunction1():
        #something here
        ....
        ....
     def myClassFunction2():
        #something here
        p1 = Process(target=myProcess1,args=(L,))
        p2 = Process(target=myProcess2,args=(L,))
        p1.start()
        p2.start()
        p1.join()
        p2.join()
        #something else here

    def myProcess1(L):
        #do something here with L

    def myProcess2(L):
        #do something here with L

I am not very good in python.....and I don't get to understand very good how the multiprocessing works. I hope someone can give me some help.

Thanks!

You need to refer to the functions as an instance method:

p1 = Process(target=self.myProcess1,args=(L,))
p2 = Process(target=self.myProcess2,args=(L,))

And in your worker functions, add self as the first argument:

def myProcess1(self, L):
    #do something awesome with L
def myProcess1(self, L):
    #do something as awesome as myProcess1 with L

Hope this helps!

I've never used multiprocessing module before, but you could use threading module...

import threading

class worker (threading.Thread):
    def __init__(self,extraArgs):
        threading.Thread.__init__(self)
        self.storeArgs = extraArgs
    def run(self):
        //add code can use self.storeArgs


a = worker(argsA)
b = worker(argsB)
a.start()  //starts concurrent thread using worker.run()
b.start()  //starts concurrent thread using worker.run()

while threading.activeCount()>1:  //loop to make sure threads are finished running
    continue    

You can either make two classes for the different methods you want to run or give the worker two methods and use if/else or case logic to decide which one to use in it's run/start method

Just define the functions before create the process. Python interpreter read the module line by line, by the time it reaches the lines creating the processes, that functions aren't defined.

I recommend you put those functions in a separated module and then import from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM