猿问

使用 multiprocessing.manager 的问题

我在使用管理器时遇到了一些问题。我想让两个进程访问一个列表,一个写一个读它。但是数据已经损坏,所以我试图在这里发布一个示例。现在,我还有另一个问题。我将一个列表作为参数传递给函数,但代码不起作用,并说我传递了两个参数而不是一个。这是代码:导入多处理


def mde(dad):

    for i in range(100):

        for j in range(10):

            dad[0] = i

            dad[1] = j

def mda(dad):

    c = 0

    while c < 1001:

        print(dad)

        c += 1


if __name__ == '__main__':

    manager = multiprocessing.Manager()

    dado = manager.list([0, 0])

    print(dado)

    p1 = multiprocessing.Process(target=mde, args=dado)

    p2 = multiprocessing.Process(target=mda, args=dado)

    p1.start()

    p2.start()

    p1.join()

    p2.join()


米琪卡哇伊
浏览 84回答 1
1回答

阿晨1998

将您的 args 作为元组发送:import multiprocessingdef mde(dad):&nbsp; &nbsp; for i in range(100):&nbsp; &nbsp; &nbsp; &nbsp; for j in range(10):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dad[0] = i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dad[1] = jdef mda(dad):&nbsp; &nbsp; c = 0&nbsp; &nbsp; while c < 1001:&nbsp; &nbsp; &nbsp; &nbsp; print(dad)&nbsp; &nbsp; &nbsp; &nbsp; c += 1if __name__ == '__main__':&nbsp; &nbsp; manager = multiprocessing.Manager()&nbsp; &nbsp; dado = manager.list([0, 0])&nbsp; &nbsp; print(dado)&nbsp; &nbsp; p1 = multiprocessing.Process(target=mde, args=(dado,))&nbsp; &nbsp; p2 = multiprocessing.Process(target=mda, args=(dado,))&nbsp; &nbsp; p1.start()&nbsp; &nbsp; p2.start()&nbsp; &nbsp; p1.join()&nbsp; &nbsp; p2.join()
随时随地看视频慕课网APP

相关分类

Python
我要回答