multiprocessing global variable updates not returned to parent

后端 未结 5 462
天命终不由人
天命终不由人 2020-11-22 10:44

I am trying to return values from subprocesses but these values are unfortunately unpicklable. So I used global variables in threads module with success but have not been ab

5条回答
  •  太阳男子
    2020-11-22 11:10

    You could also use a multiprocessing Array. This allows you to have a shared state between processes and is probably the closest thing to a global variable.

    At the top of main, declare an Array. The first argument 'i' says it will be integers. The second argument gives the initial values:

    shared_dataDV03 = multiprocessing.Array ('i', (0, 0)) #a shared array
    

    Then pass this array to the process as an argument:

    j = multiprocessing.Process(target=getDV03CclDrivers, args=('LORR',shared_dataDV03))
    

    You have to receive the array argument in the function being called, and then you can modify it within the function:

    def getDV03CclDrivers(lib,arr):  # call global variable
        arr[1]=1
        arr[0]=0
    

    The array is shared with the parent, so you can print out the values at the end in the parent:

    print 'DV03', shared_dataDV03[:]
    

    And it will show the changes:

    DV03 [0, 1]
    

提交回复
热议问题