I have two functions myFunc()
and myOtherFunc()
defined in two different files: generic.py
and no_choice.py
with two different implementations. I import them as follows into main.py
:
main.py
:
if __name__=="__main__":
config = input()
if config=="generic":
import generic as myfile
else:
import no_choice as myfile
Two functions use this import. fun1()
and fun2()
defined in main.py
.
main.py
:
def fun1():
# Do some processing
myfile.myFunc()
def fun2():
# Do some processing
myfile.myOtherFunc()
Then, I use multiprocessing to launch these two functions as parallel processes, as follows:
main.py
-> within if __name__=="__main__":
:
fun1task = multiprocessing.Process(target=fun1)
fun2task = multiprocessing.Process(target=fun2)
fun1task.start()
fun2task.start()
I get the error: NameError: name myfile is not defined
from the line myfile.myFunc()
from main.py
-> fun1()
.
How do I enable these two functions defined outside the if __name__=="__main__":
block to use the name myfile?
Pass your functions as arguments.
Change your func1 and func2 signatures to:
def fun1(func):
# Do some processing
func()
def fun2(func):
# Do some processing
func()
And then pass func
as an argument:
fun1task = multiprocessing.Process(target=fun1, args=(myfile.myFunc))
fun2task = multiprocessing.Process(target=fun2, args=(myfile.myOtherFunc))