1

I have a python file that I am calling via C#. I provide a various number of arguments, one of which is the function that I want to run. I am using multiprocessing with the daemon flag set to True. I then join() the new process with the main with the optional time limit.

Everything works as I want but I want it to be more dynamic so I don't have to have a giant list of if else if statements to decide which function to call.

So, how can I modify

p = Process(target=dc_906, args=(self.transactionCode, self.givexNum, self.amount, queue))

to use something like what is below, where self.function is the name of the defined function:

p = Process(target=self.function, args=(self.transactionCode, self.givexNum, self.amount, queue))

Full python code:

queue = mp.Queue()

if self.function == 'dc_906':
    p = Process(target=self.dc_906, args=(self.transactionCode, self.givexNum, self.amount, queue))
elif self.function == 'dc_901':
    p = Process(target=self.dc_901, args=(self.transactionCode, self.givexNum, self.amount, queue))
else:
    p = Process(target=self.dc_907, args=(self.transactionCode, self.givexNum, self.amount, queue))

p.daemon = True

p.start()

p.join(2)

if p.is_alive():
    print 'alive'

print queue.get()

size = queue.qsize()
B-M
  • 1,231
  • 1
  • 19
  • 41

2 Answers2

1

Use getattr to dynamically fetch the bound method given a string that describes it:

p = Process(target=getattr(self, self.function), args=(...)
roippi
  • 25,533
  • 4
  • 48
  • 73
1

Like the response to this question, I think you can simply use the getattr function which will create a function object from the given object and string, like so:

myfunc = getattr(self, self.function)

This means you can remove your if statements and try something like the following:

p = Process(target=getattr(self, self.function), ...)

Don't forget to still keep things comma separated.

Community
  • 1
  • 1
jbranchaud
  • 5,909
  • 9
  • 45
  • 70