The example of this article for you to share the Python multi-process shared variables related code for your reference, the specific content is as follows
From multiprocessing import Process, Managerimport Osimport time Class Mulfun (): Def __init__ (self): SELF.A = [All , 4,5] self.b = 0 SELF.C = {} Self.s = "Hello world" Self.radius = Manager (). Dict () self.radius[' a '] = self. A self.radius[' b '] = self.b self.radius[' c '] = SELF.C self.radius[' s '] = Self.s def func1 (self): self.b = S Elf.radius[' B '] for I in range: self.b + = i self.radius[' b '] = self.b time.sleep (0.5) print ' 1: ', self.radius[' B '], ': ', self.radius[' C '], Os.getpid () def func2 (self): SELF.C = self.radius[' C '] for i in [' AB ' , ' BC ', ' CD ', ' DF ', ' FG ']: self.c[i] = i + i self.radius[' c '] = SELF.C time.sleep (0.5) print ' 2: ', S Elf.radius[' B '], ': ', self.radius[' C ', Os.getpid () def run (self): Process1 = Process (Target=self.func1, args= ()) p Rocess2 = Process (Target=self.func2, args= ()) Process1.daemon = True Process2.daemon = True Process1.start () p Rocess2.start () ProceSs1.join () Process2.join () class Mulsun1:def __init__ (Self, radius): Self.radius = Radius SELF.A = radius[' a ' ] Self.s = radius[' s '] def process (self): For I, J in Enumerate (SELF.A): #self. a[i] = J * 2 #self. Radiu S[' a '] = SELF.A time.sleep (0.5) print ' 1: ', self.radius[' a '], ' & ', self.radius[' s '] class Mulsun2:def __init__ (Self, radius): Self.radius = Radius SELF.A = radius[' a '] Self.s = radius[' s '] def process (self): For I in range: Self.s = self.s + ':% s '%i if I < Len (SELF.A): self.a[i] + = i else:s Elf.a.append (i + i) self.radius[' s '] = Self.s self.radius[' a '] = SELF.A time.sleep (0.5) print ' 2: ', S Elf.radius[' A '], ' & ', self.radius[' s '] if __name__ = = ' __main__ ': aa = Mulfun () S1 = MulSun1 (aa.radius) s2 = muls Un2 (Aa.radius) Process1 = Process (target=s1.process, args= ()) Process2 = Process (target=s2.process, args= ()) process1.d Aemon = True process2.dAemon = True Process1.start () Process2.start () Process1.join () Process2.join () print "------------------------" Prin T ' process ID: ', os.getpid () print ' Done ' Print aa.radius[' a '], ' & ', aa.radius[' s '
The above is the whole content of this article, I hope that you learn Python programming help.