Python multi-process shared variables

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.