首页 > 解决方案 > modify/access python instance from external script or command

问题描述

I have a main python code running (at startup) with an instance of a class in it. I would like to be able to modify this instance (its arguments) when I want (as a user). The idea is to be able to modify it without disturbing the main code or forcing it to read an external script every 2 seconds. So, I would be able to run a bash command or a new python script that will modify this instance created in the main running script.

I use python 3 and already though/tried different things:

from terminal 1:

>>> a='c'
>>> hex(id(a))
'0x7fc9f825a378'

from terminal 2:

>>> import ctypes
>>> d = (ctypes.c_char).from_address(0x7fc9f825a378)
>>> d
Segmentation fault (core dumped)

So, how can I modify an instance in a running script from my archlinux os as a user?

Thanks a lot for your time

标签: pythoninstanceexternal

解决方案


我确实查看了有关 python 共享内存的所有信息。但是我没有找到任何解决方案来在我的 POSIX 系统上共享字典或“普通”数组。

  • sysv_ipcposix_ipc使用字符串缓冲区,使一切复杂化(需要解析)。

  • 多处理允许这样做,但从同一个脚本,没有 id/key 可以从另一个 python 实例访问它。

  • SharedArray这样做,但使用 numpy 数组。

你知道一个共享内存 python 库,它允许简单地共享一个对象,比如字典或普通列表吗?

谢谢你的时间。


推荐阅读