如何在 Python 进程之间共享数据? [英] How to share data between Python processes?

查看:30
本文介绍了如何在 Python 进程之间共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多处理为我的 Python 应用程序创建一个子进程.我想在我的父进程和子进程之间共享数据.需要说明的是,我需要异步共享这个,也就是说子进程和父进程会在代码运行期间更新数据.

I'm using multiprocessing to create a sub-process to my Python app. I would like to share data between my parent process and the child process. it's important to mention that I need to share this asynchronously, means that the child process and the parent process will update the data during the code running.

最好的方法是什么?

推荐答案

这是python文档中的一个简单示例 -

This is one simple example from python documentation -

from multiprocessing import Process, Queue

def f(q):
    q.put([42, None, 'hello'])

if __name__ == '__main__':
    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    print q.get()    # prints "[42, None, 'hello']"
    p.join()

你也可以使用管道,有关详细信息,请参阅 - https://docs.python.org/2/library/multiprocessing.html

You can use pipe as well, Refer for more details - https://docs.python.org/2/library/multiprocessing.html

这篇关于如何在 Python 进程之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆