丢失线程指针

时间:2014-08-29 09:36:36

标签: python multithreading exception

为什么线程指针会消失? 正如您在输出中看到的那样。 'obj'键被删除

def handle_threads(self):
    threads = 0
    for key, value in self.work_order.todo_list.items():
        print value
        if value['obj'].is_alive():
            threads += 1
        elif value['obj'].result == True:
            del self.work_order.todo_list[key]
    print threads
    if threads:
        sublime.set_timeout(lambda: self.handle_threads(), 100)

输出:

{'obj': <Main(Thread-93, stopped 4508)>, 'order': 'string1'}
{'obj': <Main(Thread-94, started 5704)>, 'order': 'string2'}
{'order': 'string2'}

1 个答案:

答案 0 :(得分:1)

这是因为你在这行代码中删除了键'obj'的(键,值)对:

del self.work_order.todo_list[key]

例如:

d = {'example':'hello','example2':'goodbye'}
print(d)

>>>> {'example':'hello','example2':'goodbye'}

del(d['example'])

>>>> {'example2':'goodbye'}

我不确定你在这里要做什么,但如果你想保留钥匙但要摆脱价值。使用:

self.work_order.todo_list[key] = None