繁体   English   中英

Python IPC 在两个脚本之间共享 Memory

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM