为什么我会得到不同的dict对象ID

2024-05-19 02:30:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个django(version=1.1.4,python=2.7.15)应用程序,我在其中创建一个线程A来处理其他任务,然后线程A通过subprocess.Popen创建一个进程

像这样的data.py

class AttribDict(dict):
    """
    This class defines the appscan object, inheriting from Python data
    type dictionary.

    >>> foo = AttribDict()
    >>> foo.bar = 1
    >>> foo.bar
    1
    """

    def __init__(self, indict=None, attribute=None):
        if indict is None:
            indict = {}

        # Set any attributes here - before initialisation
        # these remain as normal attributes
        self.attribute = attribute
        dict.__init__(self, indict)
        self.__initialised = True

        # After initialisation, setting attributes
        # is the same as setting an item

    def __getattr__(self, item):
        """
        Maps values to attributes
        Only called if there *is NOT* an attribute with this name
        """

        try:
            return self.__getitem__(item)
        except KeyError:
            raise AppscanDataException("unable to access item '%s'" % item)

    def __setattr__(self, item, value):
        """
        Maps attributes to values
        Only if we are initialised
        """

        # This test allows attributes to be set in the __init__ method
        if "_AttribDict__initialised" not in self.__dict__:
            return dict.__setattr__(self, item, value)

        # Any normal attributes are handled normally
        elif item in self.__dict__:
            dict.__setattr__(self, item, value)

        else:
            self.__setitem__(item, value)

    def __getstate__(self):
        return self.__dict__

    def __setstate__(self, dict):
        self.__dict__ = dict

    def __deepcopy__(self, memo):
        retVal = self.__class__()
        memo[id(self)] = retVal

        for attr in dir(self):
            if not attr.startswith('_'):
                value = getattr(self, attr)
                if not isinstance(value,
                                  (types.BuiltinFunctionType,
                                   types.FunctionType, types.MethodType)):
                    setattr(retVal, attr, copy.deepcopy(value, memo))

        for key, value in self.items():
            retVal.__setitem__(key, copy.deepcopy(value, memo))

        return retVal

flu = AttribDict()

在manage.py中,如下所示:

from data import flu

if __name__ == "__main__":
    import thread
    from tasks import task
    thread.start_new_thread(task, ())
    with open('manage.txt','w') as w:
        w.write(str(flu) + '\n' + 'id:' + str(id(flu)))
    execute_manager(settings)

task.py如下所示:

def task():
    Popen('python start.py ', shell=True)

start.py如下所示:

def statrt():
    from config import init 
    from data import flu
    init()
    with open('C:start.txt','w') as w:
        w.write(str(flu) + '\n' + 'id:' + str(id(flu)))

init.py如下所示:

from data import flu

def init():
    flu.threads = 10
    res.threadpool = ThreadPool(flu.threads)
    with open('C:/init.txt','w') as w:
        w.write(str(flu) + '\n' + 'id:' + str(id(flu)))

然后我看三个文件manage.txt{}和init.txt

像这样管理.txt:

{'threads': 10}
id: 191935728

start.txt,如下所示:

{}
id: 195720888

init.txt,如下所示:

{'threads': 10}
id: 191935728

请问这三个文件的ID为什么不同?为什么我不能在start.txt中获取线程


Tags: frompyimportselftxtidifinit

热门问题