Python:';线程。_本地对象没有属性';待办事项';
我目前正在使用线程和所有这些来编程一个基于python的数据报服务器 我遇到了以下问题:我正在使用多个分配线程将传入的包分配给不同的处理线程。在处理线程中,我使用threading.local()跟踪线程局部变量 我目前正在测试我的服务器在高负载(2秒内2000个数据包)期间的反应,并且遇到了local()对象的奇怪行为 它似乎可以正常工作一段时间,然后,在某个时候,它抛出了一个异常:Python:';线程。_本地对象没有属性';待办事项';,python,multithreading,thread-local,python-multithreading,Python,Multithreading,Thread Local,Python Multithreading,我目前正在使用线程和所有这些来编程一个基于python的数据报服务器 我遇到了以下问题:我正在使用多个分配线程将传入的包分配给不同的处理线程。在处理线程中,我使用threading.local()跟踪线程局部变量 我目前正在测试我的服务器在高负载(2秒内2000个数据包)期间的反应,并且遇到了local()对象的奇怪行为 它似乎可以正常工作一段时间,然后,在某个时候,它抛出了一个异常: Exception in thread 192.168.1.102: # <-- This is the
Exception in thread 192.168.1.102: # <-- This is the Processing Thread
Traceback (most recent call last):
File "/opt/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/volume1/home/Max/Python/MyThread/pyProcThread.py", line 37, in run
print self.loc.father
AttributeError: 'thread._local' object has no attribute 'father'
# The following three lines are debug
# This is the Allocation thread that has called the Processing thread
<Thread(192.168.1.102, started 1106023568)>
# This confirms that the queue it tries to access exists
<Queue.Queue instance at 0x40662b48>
# This is the Processing thread, which has stopped executing on exception
<Thread(192.168.1.102, stopped 1106023568)>
Exception in thread pyAlloc-0: # <-- This is the Allocation thread
Traceback (most recent call last):
File "/opt/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/volume1/home/Max/Python/MyThread/pyAllocThread.py", line 60, in run
foundThread.addTask(str(data))
File "/volume1/home/Max/Python/MyThread/pyProcThread.py", line 58, in addTask
print self.loc.todo
AttributeError: 'thread._local' object has no attribute 'todo'
和分配线程:
import threading
import pyProcThread # My processing Thread
import Queue
import time
class Thread(threading.Thread):
# Lock-Objects
threadListLock = threading.Lock()
waitListLock = threading.Lock()
alive = True;
taskQueue = Queue.Queue()
# Lists
# List of all running threads
threads = []
def threadExists(self,pIP):
"""Checks if there is already a thread with the given Name"""
for x in self.threads:
if x.name == pIP:
return x
return None
def threadTerminated(self,pThread):
"""Called when a Processing Thread terminates"""
with self.threadListLock:
self.threads.remove(pThread)
print "Thread removed"
def threadRegistered(self,pThread):
"""Registers a new Thread"""
self.threads.append(pThread)
def killThread(self):
self.alive = False
def run(self):
while(self.alive):
# print "Verarbeite Nachricht ", self.Message
# print "Von ", self.IP
try:
data, addtemp = self.taskQueue.get(True, 1)
addr, _junk = addtemp
with self.threadListLock:
foundThread=self.threadExists(str(addr))
# print "Thread " + self.name + " verarbeitet " + data
if (foundThread!=None):
#print "recycling thread"
foundThread.addTask(str(data))
else:
print "running new Thread"
t = pyProcThread.Thread(self)
t.name = str(addr)
t.addTask(str(data))
t.start()
self.threadRegistered(t)
self.taskQueue.task_done()
except Queue.Empty:
pass
print self.name, "terminating..."
with self.threadListLock:
for thread in self.threads:
thread.addTask(thread)
完整输出,包括所有调试打印:
running new Thread
Konstruktor ausgefuehrt
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, initial)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
Exception in thread 192.168.1.102:
Traceback (most recent call last):
File "/opt/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/volume1/home/Max/Python/MyThread/pyProcThread.py", line 37, in run
print self.loc.father
AttributeError: 'thread._local' object has no attribute 'father'
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, stopped 1106023568)>
Exception in thread pyAlloc-0:
Traceback (most recent call last):
File "/opt/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/volume1/home/Max/Python/MyThread/pyAllocThread.py", line 60, in run
foundThread.addTask(str(data))
File "/volume1/home/Max/Python/MyThread/pyProcThread.py", line 58, in addTask
print self.loc.todo
AttributeError: 'thread._local' object has no attribute 'todo'
Terminating main
192.168.1.102
DEINEMUDDA sent to 192.168.1.102 : 8082
pyAlloc-1 terminating...
<Thread(192.168.1.102, stopped 1106023568)>
<Queue.Queue instance at 0x40662b48>
运行新线程
ausgefuehrt康斯特酒店
线程192.168.1.102中的异常:
回溯(最近一次呼叫最后一次):
文件“/opt/lib/python2.7/threading.py”,第552行,在引导程序内部
self.run()
文件“/volume1/home/Max/Python/MyThread/pyProcThread.py”,第37行,运行中
打印self.loc.father
AttributeError:“线程.\u local”对象没有属性“父”
线程pyAlloc-0中出现异常:
回溯(最近一次呼叫最后一次):
文件“/opt/lib/python2.7/threading.py”,第552行,在引导程序内部
self.run()
文件“/volume1/home/Max/Python/MyThread/pyAllocThread.py”,第60行,运行中
foundThread.addTask(str(数据))
文件“/volume1/home/Max/Python/MyThread/pyProcThread.py”,第58行,在addTask中
打印self.loc.todo
AttributeError:“thread.\u local”对象没有属性“todo”
端接干线
192.168.1.102
DEINEMUDDA发送到192.168.1.102:8082
pyAlloc-1终止。。。
正如您在日志中所看到的,虽然在处理线程的主要功能中从未达到print”处理
,但在一段时间内,一切似乎都正常工作
我已经在web和StackOverflow上搜索过类似的问题,但没有找到任何问题。提前感谢您的帮助,请原谅我的编码风格,我现在只编写了几天Python,仍然在学习Python的一些特性
编辑:当然,这个服务器还有更多的功能。有一个主线程正在接收数据包并将其发送到分配线程,还有一堆其他的东西在后台。此外,服务器还远没有完成,但我想在开始其他事情之前让接收工作正常。线程化。本地e> 表示线程本地数据的类。线程本地数据是其值特定于线程的数据,因此只能用于本地线程(创建
local()
object的线程)。换句话说,只有设置该值的线程才能看到该值
由于您在处理线程的
\uuuu init\uuuu
方法中设置了loc.parent
值,但在分配器线程内执行,因此处理线程的线程局部变量将仅在分配器线程中可用。因此它在运行()中不起作用处理线程的
,因为它将由不同的线程(而不是分配器)执行。这很有趣,谢谢。如果我删除打印self.loc.parent,他也有一个关于self.loc.todo的问题。我想这有同样的原因。我如何才能让它工作(将parent
和todo
属性获取到loc
中,这两个属性都可以从处理线程中访问)?还有一个python帮助页面详细解释了整个机制吗?再次感谢。您可以将值设置为Thread
对象的常规属性,因此self.father=pThread
。我相信father不是执行特定于线程的,而是执行特定于线程实例的。嗯,这实际上是我在尝试执行之前的工作解决方案使用本地对象“正确”。但由于它似乎是self.attribute
的正确方法,我可以继续使用它(我觉得这是一个肮脏的解决方法,因为我第一次尝试时无法让本地对象工作)。再次感谢。
running new Thread
Konstruktor ausgefuehrt
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, initial)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
Exception in thread 192.168.1.102:
Traceback (most recent call last):
File "/opt/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/volume1/home/Max/Python/MyThread/pyProcThread.py", line 37, in run
print self.loc.father
AttributeError: 'thread._local' object has no attribute 'father'
<Thread(192.168.1.102, started 1106023568)>
<Queue.Queue instance at 0x40662b48>
<Thread(192.168.1.102, stopped 1106023568)>
Exception in thread pyAlloc-0:
Traceback (most recent call last):
File "/opt/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/volume1/home/Max/Python/MyThread/pyAllocThread.py", line 60, in run
foundThread.addTask(str(data))
File "/volume1/home/Max/Python/MyThread/pyProcThread.py", line 58, in addTask
print self.loc.todo
AttributeError: 'thread._local' object has no attribute 'todo'
Terminating main
192.168.1.102
DEINEMUDDA sent to 192.168.1.102 : 8082
pyAlloc-1 terminating...
<Thread(192.168.1.102, stopped 1106023568)>
<Queue.Queue instance at 0x40662b48>