Modify object via Python multiprocessing.Pool: strange behavior -


I have an object with two characteristics: a dict and an int. When I modify the object using a forked process through multiprocessing. Poll, I take the object back with the modified int attribute, but the word is not modified. Why is it like this? Some Diff Forks () from Multiprocessing Import: For some classes () I in range (10): someObject.method (i) print ("In the fork, some object is Dct =% s and nbr =% i" % (SomeObject.dct, someObject.nbr)) Some object def test (): pool = pool (process = 1) result = pool.apple (func = fork) prints ("main, some objects in DCT =% s and NBR =% i "% (result.dct, result.nbr)) class some object (dct = {} nbr = 0 def method (self, nbr): self.dct [nbr] = nbr self.nbr + = Nbr if __name __ == '__ Main__ ': test ()

in output:

, fork, someObject in dct = {0: 0, 1: 1, 2: 2, 3: 3 In main, some objects in dct = {} and nbr = 45, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} and NBR = 45

SomeClass.dct and SomeClass.nbt

From the hair process (es)

The Causes NBR has been updated but dct is that nbr actually becomes an example variable When you self.nbr + = nbr , which gets spicy and the original process is sent back but you have never specified anything at self.dct , Hence the self.dct (which in fact refers to SomeClass.dct ) does not get spicy.

You see this by defining some class (object) class

Can: dct = {} NBR = 0 DEF method (self, NBR): DCTT itself [NBR] = NBR self. NBR + = NBR DF __getstate __ (self): Reserve = SED.__Direct Print ("Pikale", Reserva) Return Resize

Print:

 In the code, in some objects, DCT = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} and NBR = 45 ('cooked', {'NBR': 45}) In the main, some objects have DCT = {} and NBR = 45  

You can force dct to decorate by specifying for:

  class to some class (ob) Ject): dct = {} Anbiar = 0 DEF method (self Anbiar): Self Disititi [Anbiar] = Anbiar own DCT = independent self. NBR + = NBR  

In this case, in some objects DCT = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} and NBR = 45 ('spicy', 'NBR': 45, 'DCT': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4), 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}}} In the main, some objects have DCT = { 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} and NBR = 45 < / Pre>

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -