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()