問題描述
我正在使用多處理為我的 Python 應(yīng)用程序創(chuàng)建一個子進程.我想在我的父進程和子進程之間共享數(shù)據(jù).需要說明的是,我需要異步共享這個,也就是說子進程和父進程會在代碼運行期間更新數(shù)據(jù).
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()
你也可以使用管道,有關(guān)詳細信息,請參閱 - 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
這篇關(guān)于如何在 Python 進程之間共享數(shù)據(jù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!