Python和线程处理:为什么“大"字?列表实体从其他方法访问时会迷路? [英] Python and threading: Why does "large" list entity get lost when accessing from other method?

查看:41
本文介绍了Python和线程处理:为什么“大"字?列表实体从其他方法访问时会迷路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个方法(foo)中创建一个大型实体(列表)并将其绑定到self.result. 从第二种方法(transmit)访问该实体的尝试从某个大小(列表中150,000至155,000个字符之间的大小)开始失败.从transmit内部进行打印(print self.result)将使我保持无. 我想这很重要:self.foo是在单独的线程中直接调用的. 请帮忙.如何从单独的线程中将这样的大"实体返回主线程,而又没有这样的限制?

A large entity (list) is created in one method (foo) and bound to self.result. The attempt to access this entity in a second method (transmit) fails starting at a certain size (something between 150,000 and 155,000 characters in the list). Printing (print self.result) from inside transmit leaves me with None. I guess this is important: self.foo is directly called in a separate thread.
Please help. How do I get such "large" entity from a separate thread back into the main thread without such limitation?

...

    def apply(self):
        self.get_data()
        self.start_foo_thread()

    def start_foo_thread(self):
        self.foo_thread = threading.Thread(target=self.foo)
        self.foo_thread.daemon = True
        self.progressbar.start()
        self.foo_thread.start()
        self.master.after(20, self.check_foo_thread)

    def check_foo_thread(self):
        if self.foo_thread.is_alive():
            self.master.after(20, self.check_foo_thread)
        else:
            self.progressbar.stop()

    def foo(self): 
        s = self.stringinput
        n = self.numberinput
        list = multiply_str_into_list(s, n)
        self.result = list_to_text(list)
        print self.result # output is not None 

    def transmit(self):
        print self.result # output is None for more than about 155,000 characters in the list
        return self.result

def multiply_str_into_list(string, n): #takes a string and multiplies it by n and writes into list
    n_string = []
    for i in range(0,n):
        n_string.append(string)
    return n_string

def list_to_text(list): #takes a list as input and joins it into str with each list item on a new line
    a = '\n'.join(list)
    return a

推荐答案

您实际上并没有提供足够的信息来重现该问题,更不用说调试它了,但是我想在某个时候,您正在做这样的事情:

You don't really provide enough information to reproduce the problem, let alone debug it, but my guess is that at some point, you are doing something like this:

self.result = self.result.append(x)

由于.append()在适当位置修改了列表,并返回None,因此这将破坏对列表的引用.也不必是.append() -变异列表的所有方法都返回None.

Since .append() modifies the list in place, and returns None, this will clobber the reference to the list. It needn't be .append() either -- all of the methods that mutate lists return None.

关于为什么它仅以一定大小发生的原因,也许您有像上面这样的代码仅以一定大小被触发,否则它是一条红鲱鱼.

As to why it is happening only at a certain size, perhaps you have some code like the above that is triggered only at a certain size, or else it is a red herring.

这篇关于Python和线程处理:为什么“大"字?列表实体从其他方法访问时会迷路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆