python - 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:
- write a method with an opened socket listening in the class corresponding to the instance. Seems overkill to me.
- be able to access the instance from its memory address with some tools such as ctypes and id. But I got errors in this way (Segmentation fault)
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
解决方案
我确实查看了有关 python 共享内存的所有信息。但是我没有找到任何解决方案来在我的 POSIX 系统上共享字典或“普通”数组。
sysv_ipc或posix_ipc使用字符串缓冲区,使一切复杂化(需要解析)。
多处理允许这样做,但从同一个脚本,没有 id/key 可以从另一个 python 实例访问它。
SharedArray这样做,但使用 numpy 数组。
你知道一个共享内存 python 库,它允许简单地共享一个对象,比如字典或普通列表吗?
谢谢你的时间。
推荐阅读
- python - 1 Integer 似乎正在取另一个 Python 的值
- python - Python:字典内的嵌套列表?
- c# - 为什么编译器不抱怨并非所有路径都返回?
- python-3.x - 创建具有 3 个级别的列表,包含子列表值的每个组合
- typescript - TypeScript variadic ...args 与这些类型的返回数组
- c# - 有没有办法给 c# 枚举一个允许值的“范围”?
- c - 从 C 中的 if 语句返回一个字符串
- c# - 用另一个数组的值随机分配数组值
- javascript - 反应子组件不会在状态更改时重新渲染
- android - 发布到 Firestore 时对 radioButton 使用相同的值