如何读取(并放入新的变量中)存储在特定内存地址中的数据?例如,我知道:nfqueue.queue; proxy of Swig Object of type queue * at 0xabd2b00 我希望将数据存储在新变量中的0xabd2b00处,以便我可以工作和使用对...

如何读取(并放入新的变量中)存储在特定内存地址中的数据?
例如,我知道:
<nfqueue.queue; proxy of <Swig Object of type 'queue *' at 0xabd2b00> >
我希望将数据存储在新变量中的0xabd2b00处,以便我可以工作和使用对象的所有功能.假设我无权访问创建此对象的原始变量.
更新:
以上问题已得到回答,所以我更新了我的问题.
假设我有两个python文件:file1.py和file2.py
File1.py:
.... rest of the code ....
class new_thread(threading.Thread):
def __init__(self, obj):
self.obj = obj
threading.Thread.__init__(self)
def run(self):
str = 'python file2.py'
args = shlex.split(str3)
tmp = subprocess.Popen(args, stdout=open('/dev/null','w'), stderr=open('/dev/null', 'w'))
.... rest of the code ....
在某个时候线程new_thread被调用.
File2.py:
kolejka = nfqueue.queue()
在这里,队列被创建,绑定和打开.然后执行无限循环进行监听.结束它的唯一方法是取消绑定并关闭kolejka,但我希望file1.py这样做,因为它是一个“主”程序. new_thread完成后,如何从文件中检索已初始化的kolejka以正确关闭队列?
当我尝试:
from file2 import kolejka
该脚本从头开始执行创建队列的所有过程(尚未将其编写为函数).
解决方法:
您不能-无法从特定地址读取数据.如果您没有(或无法检索)对您感兴趣的对象的引用,那么您就不走运了.
此外,即使您可以从给定地址读取数据,也无济于事,因为除非您引用了原始对象,否则您将无法知道从哪个地址读取数据.然后,您无需首先从内存中读取原始数据.
更新-如何干净地终止子进程
有几种方法可以在Python的进程之间共享内存(例如multiprocessing模块).但是,这似乎对您的问题有些矫kill过正.由于您是从new_thread内部启动file2进程的,因此最简单的解决方案可能是使用signal模块让new_thread告诉main2退出时file2进程退出.
这使file2.py可以在关闭前执行所需的任何清理操作,这也是一种干净的解决方案,因为file1.py无需了解有关如何关闭file2.py的详细信息,从而使您的代码更具模块化且易于维护.
file1.py
def run(self):
...
child_process = subprocess.Popen(args, ...)
...
# time to quit - tell file2 to terminate
child_process.terminate()
file2.py
import signal
import sys
...
kolejka = nfqueue.queue()
...
def sigterm_handler(signum, frame):
# close kolejka and do any other cleanup needed, then do:
sys.exit()
# Make sure sigterm_handler() is run when file1.py tells us
# to quit using child_process.terminate()
signal.signal(signal.SIGTERM, sigterm_handler)
本文标题为:python-从特定的内存地址读取数据/对对象的引用


基础教程推荐
- 详解Python中的List 2 2023-08-11
- Python的函数使用示例详解 2023-08-08
- 解决Python复杂zip文件的解压问题 2023-08-08
- linux@ubuntu工具Eclipse配置python的pydev开发环境 2023-09-03
- python数据结构之递归方法讲解 2023-08-08
- python列表详情 2023-08-09
- 教你使用Python画棵圣诞树完整代码 2023-08-11
- 基于Opencv图像识别实现答题卡识别示例详解 2023-08-09
- python通过pillow识别动态验证码的示例代码 2023-08-08
- Python中最神秘missing()函数介绍 2023-08-09