interprocess communication -3 (Manager)

Source: Internet
Author: User

The Manager can implement the sharing of lists, dictionaries, variables, locks, semaphores, events, and other data. The manager is locked by default.

From multiprocessing import Process, Managerimport osdef f (d, L):    d[1] = ' 1 '    d[' 2 '] = 2    d[0.25] = None    d[o S.getpid ()]=os.getpid ()    l.append (Os.getpid ()) #获取进程号    print (l) if __name__ = = ' __main__ ': With    Manager () As manager: #manager =manager ()        d = manager.dict () #生成一个在多个进程之间可以传递共享的字典,        L = manager.list (range (5)) # Generates a list that can be passed and shared across multiple processes, with 5 elements        p_list = [] #存放结果 for        i in range:            p = Process (Target=f, args= (d, L)) C12/>p.start ()            p_list.append (p) for        res in p_list: #等待结果            res.join ()        print (d)        print (L)

Operation Result:

C:\abccdxddd\Oldboy\python-3.5.2-embed-amd64\python.exe C:/abccdxddd/oldboy/py_exercise/day10/manager_tes.py[0, 1, 2, 3, 4, 12836][0, 1, 2, 3, 4, 12836, 11324][0, 1, 2, 3, 4, 12836, 11324, 15000][0, 1, 2, 3, 4, 12836, 11324, 15000, 95 76][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476, 13960][0, 1, 2, 3, 4 , 12836, 11324, 15000, 9576, 14476, 13960, 14504][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476, 13960, 14504, 4208][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476, 13960, 14504, 4208, 12984][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476, 13960, 14504, 4208, 12984, 11724]{0.25:none, 1: ' 1 ', 12,836:12,836, 13,960:13,960, 11,724:11,724, 12,984:12,984, 14,476:14,476 , ' 2 ': 2, 9,576:9,576, 4,208:4,208, 14,504:14,504, 15,000:15,000, 11324:11324}[0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 1447 6, 13960, 14504, 4208, 12984, 11724]process finished with exit code 0

interprocess communication -3 (Manager)

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.