I am new to multiprocessing concepts in python and I have problem accessing variables when I try to include multiprocessing in my code. Sorry if Iam sounding naive, but I just cant figure it out. Below is a simple version of my scenario.
class Data:
def __init__(self):
self.data = "data"
def datameth(self):
print self.data
print mainvar
class First:
def __init__(self):
self.first = "first"
def firstmeth(self):
d = Data()
d.datameth()
print self.first
def mymethod():
f = First()
f.firstmeth()
if __name__ == '__main__':
mainvar = "mainvar"
mymethod()
When I run this, its running fine and gives the output:
data
mainvar
first
But when I try to run mymethod()as a process
from multiprocessing import Process
class Data:
def __init__(self):
self.data = "data"
def datameth(self):
print self.data
print mainvar
class First:
def __init__(self):
self.first = "first"
def firstmeth(self):
d = Data()
#print mainvar
d.datameth()
print self.first
def mymethod():
f = First()
f.firstmeth()
if __name__ == '__main__':
mainvar = "mainvar"
#mymethod()
p = Process(target = mymethod)
p.start()
I get an error like this:
NameError: global name 'mainvar' is not defined
The point is, Iam not able to access mainvar from inside First class or Data class.
What am I missing here?
Edit: Actually in my real scenario, it is not just declaring mainvar, it is the return value of a method after some processing.
if __name__ == '__main__':
***some other stuff***
mainvar = ***return value of some method**
p = Process(target = mymethod)
p.start()
Edit 2: As @dciriello mentioned in comments, It is working fine in Linux but not in Windows :(