从特定内存地址/对象引用中读取数据

如何读取(并放入新变量)存储在特定内存地址的数据? 比如我知道:
  <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中的进程之间共享内存(例如多处理模块)。但是,这对您的问题似乎有点过分。由于你从
new_thread
开始
file2
过程,最简单的解决方案可能是使用信号模块让
new_thread
告诉
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)
    

要回复问题请先登录注册