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

查看:267
本文介绍了如何在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.

执行此操作的最佳方法是什么?

What would be the best way to perform that?

推荐答案

这是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天全站免登陆