python Manager dict

多进程,共享manager.dict,qthread读取有问题,报错,dictproxy,不让读取数据,2019.01.17无解

 

from multiprocessing import Process, Manager

def test(m):
    # m[0]['id'] = 2 错误写法
#正确写法
    hack = m[0]
    hack['id'] = 2
    m[0] = hack
if __name__ == '__main__':
    manager = Manager()
    m = manager.list()
    m.append({'id': 1})

    p = Process(target=test,args=(m,))
    p.start()
    p.join()
    print(m[0])

 

 

# coding:utf-8
import threading
import time
from multiprocessing import Process, Manager

def test(m):
    # m[0]['id'] = 2 错误写法
#正确写法

    for i in range(10):
        hack = m['id']
        hack.put(str(i))
    # hack['id'] = 2
    # m[0] = hack

def action(arg):
    for i in range(10):
        time.sleep(0.01)
        a=m['id']
        print(a.get())


if __name__ == '__main__':
    manager = Manager()
    m = manager.dict()
    q=manager.Queue(50)
    m={'id': q}

    p = Process(target=test,args=(m,))
    p.start()
    q.put('111')
    t = threading.Thread(target=action, args=(m,))
    t.start()
    # p.join()
    a = m['id']
    print(a.get())
    q.put('353')

 

无解代码:

 

# coding:utf-8
import threading
import time
from multiprocessing import Process, Manager

from PyQt5.QtCore import QThread


def test(m):
    # m[0]['id'] = 2 错误写法
#正确写法
    for i in range(10):
        hack = m['id']
        hack.put(str(i))
    # hack['id'] = 2
    # m[0] = hack

class Savepic_thread(QThread):
    def __init__(self, parent=None,q=None):
        super().__init__()
        self.q=q
    def run(self):

        for i in range(10):
            a=self.q['id']
            print(a.get())



if __name__ == '__main__':
    manager = Manager()
    m = manager.dict()
    q=manager.Queue(50)
    m={'id': q}

    p = Process(target=test,args=(m,))
    p.start()
    q.put('111')
    # t = threading.Thread(target=action, args=(m,))
    t = Savepic_thread(m)
    t.start()
    p.join()
    a = m['id']
    print(a.get())
    q.put('353')
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页