![](/img/trans.png)
[英]IPC shared memory across Python scripts in separate Docker containers
[英]Python IPC Shared Memory between two scripts
我正在嘗試為 Raspberry Pi3 開發應用程序。 事實上2個應用程序。 應用程序 1 將是一個簡單的 gpio 讀取,計算特定 gpio 輸入變高的次數並登錄到文件中。 應用 2 將在屏幕上顯示 GPIO 狀態。 我希望應用程序 1 持續運行並記錄數據。 這就是我將其與基於 UI 的應用程序分開的原因。 現在我想從應用程序 1 中獲取 gpio 狀態 - 數據將是 pin1State、pin1HighCount、pin2State、pin2HighCount。 所有這些都是整數。 應用程序 2 應從應用程序 1 獲取數據並顯示在基於 pyqt5 的 UI 屏幕中。 我嘗試按照這個示例IPC 在單獨的 Docker 容器中跨 Python 腳本共享 memory但我發現它是基於字典的數據交換,而且它不是連續的或幾乎實時的數據交換。 首先填充字典,然后加載到 server.py 我無法在其他地方找到有關此方法的太多信息。 我喜歡不使用文件(臨時或其他)的基於本地主機的套接字方法。 但我無法獲得連續數據。 此外,是否可以使用列表甚至單個 integer 變量而不是字典。 我擔心通過不斷更新(追加)到字典,如果腳本長時間運行我的代碼如下 Server.py
from multiprocessing.managers import SyncManager
import multiprocessing
patch_dict = {}
def load_patch_dict():
value = input("Press key to continue \n")
for i in range(0,2000):
patches = 1
patch_dict.update([(i, i)])
print("Patch Print ",i," . ",patch_dict)
#print(patch_dict)
def get_patch_dict():
return patch_dict
class MyManager(SyncManager):
pass
if __name__ == "__main__":
load_patch_dict()
port_num = 5000
MyManager.register("patch_dict", get_patch_dict)
manager = MyManager(("127.0.0.1", port_num), authkey=b"password")
# Set the authkey because it doesn't set properly when we initialize MyManager
multiprocessing.current_process().authkey = b"password"
manager.start()
input("Press any key to kill server".center(50, "-"))
manager.shutdown()
客戶端.py
rom multiprocessing.managers import SyncManager
import multiprocessing
import sys, time
class MyManager(SyncManager):
pass
# MyManager.register("patch_dict")
if __name__ == "__main__":
port_num = 5000
MyManager.register("patch_dict")
manager = MyManager(("127.0.0.1", port_num), authkey=b"password")
multiprocessing.current_process().authkey = b"password"
manager.connect()
patch_dict = manager.patch_dict()
keys = list(patch_dict.keys())
#print("Keys ", keys)
value = input("Press key to continue \n")
do_loop = True
i=1
for key in keys:
image_patches = manager.patch_dict.get(key)
print("This is ",image_patches)
好的。 目標是在 2 個 python 腳本之間共享數據。 我放棄了從另一個問題中獲取的上述解決方案。 相反,我采用了不同的方法:使用 memcache 我首先使用 pip 安裝 python-memcached 然后我使用以下代碼程序 A
import time
import memcache
def run_loop():
client = memcache.Client([('127.0.0.1', 5000)])
value = input("Press key to continue \n")
for i in range(0,20000):
sendvalue = "It Is " + str(i)
client.set('Value', sendvalue)
print("sent value ",sendvalue)
time.sleep(0.005)
if __name__ == "__main__":
run_loop()
方案 B
import sys, time
#import pymemcache
import memcache
if __name__ == "__main__":
#print("Keys ", keys)
value = input("Press key to continue \n")
do_loop = True
i=1
client = memcache.Client([('127.0.0.1', 5000)])
#for key in keys:
while do_loop:
ret_value = client.get('Value')
if ret_value != None:
print("Value returned is ",ret_value)
重要 *** 啟動 memcached。 我正在使用 Linux 來運行這些程序。 不確定 windows 環境
就我在 linux 機器上的測試而言,數據正在正確共享。 當然,我必須測試移動列表、數據交換等,我認為這應該不是問題。 我嘗試了如何在 python 中跨腳本共享變量? 我遇到了一些問題。 當然,memcached 應該正在運行但這確實有幫助: https://realpython.com/python-memcache-efficient-caching/
事實上,我們也可以為緩存設置過期時間。 太好了,我還在學習。 如果我在某處錯了,請糾正我謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.