简体   繁体   中英

How can I access a multiprocessing list in a python file from another python file?

i have a file check.py as:

import multiprocessing

class append_data:

    def __init__(self,shared_data):
        self.shared_data = shared_data
        self.shared_data.append(123)
        self.shared_data.append(456)

def func(shared_data):

        appenddata = append_data(shared_data)

if __name__ == '__main__':

        manager = multiprocessing.Manager()
        shared_data = manager.list()

        process1 = multiprocessing.Process(target=func, args= (shared_data,))
        process1.start()
        process1.join()
        print(shared_data)

Is there a way to get this 'shared_data' value on another file check1.py in the same directory.

in python when we import a module all of the code inside it is executed except the if __name__ == '__main__': block.

this block is only executed if the module itself is run.

if you want to use the shared_data variable in another module, just place the code used to calculate shared_data outside the if __name__ == '__main__': block,


manager = multiprocessing.Manager()
shared_data = manager.list()

if __name__ == '__main__':

        process1 = multiprocessing.Process(target=func, args= (shared_data,))
        process1.start()
        process1.join()
        print(shared_data)

now in "check1.py"

import check

shared_data = check.shared_data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM