Python multiprocessing, passing an object reference containig a semaphore

前端 未结 1 1516
情深已故
情深已故 2021-01-02 17:49

I\'ve a scenario like this: I\'ve created an object of the class element containing a semaphore.

import multiprocessing as mpr

class Element(object):
    de         


        
相关标签:
1条回答
  • 2021-01-02 18:34

    I don't think you understood how the multiprocessing module works.

    When you send something through the pipe, it gets pickled and then unpickled in the subprocess. This means that the subprocess actually has a copy of the original object! That's why the change is "lost". Adding a semaphore wont change anything.

    If you want an object in shared memory you should use multiprocessing.Value, even though this does not handle arbitrary types. Probably multiprocessing.Manager is what you are looking for.

    An other way would be to send a response to the main process providing the modified object.

    0 讨论(0)
提交回复
热议问题