在两个(或多个).py文件之间共享python对象

在两个(或多个).py文件之间共享python对象,python,memory,memory-management,Python,Memory,Memory Management,我希望能够运行一个python文件(file1),它只需将几个大文件作为python对象加载到内存中,然后使用不同的python文件(file2)访问这些相同的对象,而无需再次将文件重新加载到内存中。其动机是,我希望能够迭代地修改/开发file2,而不必在每次迭代中浪费时间重新加载相同的大文件 在Jupyter笔记本中,通过运行一次加载文件的单元格,可以轻松实现这一点;然后,笔记本中的所有其他单元格都可以访问这些对象。我希望能够在单独的python文件之间建立相同的串扰 有没有办法在单独的.py

我希望能够运行一个python文件(
file1
),它只需将几个大文件作为python对象加载到内存中,然后使用不同的python文件(
file2
)访问这些相同的对象,而无需再次将文件重新加载到内存中。其动机是,我希望能够迭代地修改/开发
file2
,而不必在每次迭代中浪费时间重新加载相同的大文件

在Jupyter笔记本中,通过运行一次加载文件的单元格,可以轻松实现这一点;然后,笔记本中的所有其他单元格都可以访问这些对象。我希望能够在单独的python文件之间建立相同的串扰

有没有办法在单独的.py文件之间建立笔记本电脑Jupyter风格的python对象单元间共享?


(编辑以包含示例)

下面是一个示例场景;假设有两个文件:

file1.py

from sklearn.externals import joblib
q = joblib.load('large_dict') #load a large dictionary that has been saved to disk; let's say it takes 2 minutes to load
p = joblib.load('large_dict2') #load another large file; another 2 minutes load time
#notice that the q, p objects are never loaded, but they are accessible 
#(this is possible if the contents of these py files are in separate cells
#in a Jupyter notebook)
for experiment, chromosome in q.iteritems():
    #stuff to work with dict object
for experiment, chromosome in p.iteritems():
    #stuff to work with dict object
file2.py

from sklearn.externals import joblib
q = joblib.load('large_dict') #load a large dictionary that has been saved to disk; let's say it takes 2 minutes to load
p = joblib.load('large_dict2') #load another large file; another 2 minutes load time
#notice that the q, p objects are never loaded, but they are accessible 
#(this is possible if the contents of these py files are in separate cells
#in a Jupyter notebook)
for experiment, chromosome in q.iteritems():
    #stuff to work with dict object
for experiment, chromosome in p.iteritems():
    #stuff to work with dict object
我想做什么

python文件1.py

一次,然后执行

python文件2.py


任意次数(即迭代修改
file2
中的代码)。请注意,在此场景中,
file1.py
中创建的对象可供
file2.py
访问。我的问题是:这种情况可能吗?对象不属于特定的文件。它们所属的类或生成它们的函数可能来自“物理”驻留在不同文件中的模块,但这并不重要。只要您处于单个python解释器会话中,就不需要复制对象

有一个问题:如果您有一个模块,并且您想要修改它,并且您想要将该模块的最新版本加载到已经导入该模块的运行python解释器中,它将“拒绝”这样做(这实际上是一个性能优化,以便您可以多次保存导入模块)

您可以通过python3中的
importlib.reload
或python2中的
reload
内置命令“强制”python解释器重新加载模块。有关更多信息,请参阅

在您的示例中,数据不会共享,因为您有两个不同的python进程。数据不在两个进程之间共享(通常情况下,如果您有两个“C进程”(用C编写的程序),它们也不会共享任何数据。它们可以相互发送数据,但这需要复制,这是您希望避免的)

但是您可以将数据和函数导入到“共享”python解释器中

file1.py:

from sklearn.externals import joblib
q = joblib.load('large_dict') #load a large dictionary that has been saved to disk; let's say it takes 2 minutes to load
p = joblib.load('large_dict2') #load another large file; another 2 minutes load time
file2.py:

from file1 import q,p
#notice that the q, p objects are never loaded, but they are accessible 
#(this is possible if the contents of these py files are in cells
#in a Jupyter notebook)
    for experiment, chromosome in q.iteritems():
        #stuff to work with dict object
    for experiment, chromosome in p.iteritems():
        #stuff to work with dict object
file3.py:

import file2 
# file2.py will be executed once when importing
# attributes will be accessible by file2.attribute_name

inp = ""
while inp != "quit":
    inp = input("Type anything to reload or 'quit' to quit")

    # 'import file2' would **not** execute file2 because imports
    # are only done once. Use importlib.reload (or reload in python2)
    # to "force" reloading of module 
    importlib.reload(file2)
然后您可以通过
pythonfile3.py“开始执行”,它将等待您重新加载file2的任何输入。当然,您可以使重新加载任意文件的机制变得复杂,例如,每当file2.py发生更改时重新加载(这可能会有所帮助)

另一种方法是使用

file4.py:

import importlib
import file2
def reload():
     importlib.reload(file2)
然后使用
python-i file4.py
。然后,您将使用普通的python解释器,但
reload()
将重新加载(即执行)文件2


请注意,您可以在jupyter/ipython笔记本中执行相同的操作。甚至有一些神奇的命令来帮助你。请参阅以获取有关该属性的详细信息。

对象不属于特定文件。它们所属的类或生成它们的函数可能来自“物理”驻留在不同文件中的模块,但这并不重要。只要您处于单个python解释器会话中,就不需要复制对象

有一个问题:如果您有一个模块,并且您想要修改它,并且您想要将该模块的最新版本加载到已经导入该模块的运行python解释器中,它将“拒绝”这样做(这实际上是一个性能优化,以便您可以多次保存导入模块)

您可以通过python3中的
importlib.reload
或python2中的
reload
内置命令“强制”python解释器重新加载模块。有关更多信息,请参阅

在您的示例中,数据不会共享,因为您有两个不同的python进程。数据不在两个进程之间共享(通常情况下,如果您有两个“C进程”(用C编写的程序),它们也不会共享任何数据。它们可以相互发送数据,但这需要复制,这是您希望避免的)

但是您可以将数据和函数导入到“共享”python解释器中

file1.py:

from sklearn.externals import joblib
q = joblib.load('large_dict') #load a large dictionary that has been saved to disk; let's say it takes 2 minutes to load
p = joblib.load('large_dict2') #load another large file; another 2 minutes load time
file2.py:

from file1 import q,p
#notice that the q, p objects are never loaded, but they are accessible 
#(this is possible if the contents of these py files are in cells
#in a Jupyter notebook)
    for experiment, chromosome in q.iteritems():
        #stuff to work with dict object
    for experiment, chromosome in p.iteritems():
        #stuff to work with dict object
file3.py:

import file2 
# file2.py will be executed once when importing
# attributes will be accessible by file2.attribute_name

inp = ""
while inp != "quit":
    inp = input("Type anything to reload or 'quit' to quit")

    # 'import file2' would **not** execute file2 because imports
    # are only done once. Use importlib.reload (or reload in python2)
    # to "force" reloading of module 
    importlib.reload(file2)
然后您可以通过
pythonfile3.py“开始执行”,它将等待您重新加载file2的任何输入。当然,您可以使重新加载任意文件的机制变得复杂,例如,每当file2.py发生更改时重新加载(这可能会有所帮助)

另一种方法是使用

file4.py:

import importlib
import file2
def reload():
     importlib.reload(file2)
然后使用
python-i file4.py
。然后,您将使用普通的python解释器,但
reload()
将重新加载(即执行)文件2


请注意,您可以在jupyter/ipython笔记本中执行相同的操作。甚至有一些神奇的命令来帮助你。有关这方面的更多信息,请参阅。

您所说的“在文件之间共享对象”是什么意思?你是说“线间”吗?我想问题很清楚,因为它是不,不是。我有同样的问题。“文件”在Python中不能做任何事情。它可能是主模块,也可能被导入,等等,但它的活动取决于此,而不是取决于它是一个“文件”。请澄清。“如果有多人说你的问题不清楚,那就是不清楚。”Ryan。哦,好的。尽管事实上,文件与对象没有直接的联系,“在文件之间共享对象”这个问题根本没有意义