在python中从线程检索数据
我有一个python线程处理接收OSC数据包。。。 我需要在我的主函数中从osc检索数据。如何从线程中获取数据? 下面是演示我的问题的代码: 尝试使用类,但仍然“未定义数据”在python中从线程检索数据,python,multithreading,Python,Multithreading,我有一个python线程处理接收OSC数据包。。。 我需要在我的主函数中从osc检索数据。如何从线程中获取数据? 下面是演示我的问题的代码: 尝试使用类,但仍然“未定义数据” 提前感谢使用标准库中的a或使用全局变量。您的意思是要使用函数main中函数打印处理程序中的参数数据内容吗?更改plouf=data.receivement()toplouf=receipt.data谢谢Thomas!现在它给了我这个错误:receive实例没有属性'data',我一直在搜索你的代码。将def printin
提前感谢使用标准库中的a或使用全局变量。您的意思是要使用函数main中函数打印处理程序中的参数数据内容吗?更改
plouf=data.receivement()
toplouf=receipt.data
谢谢Thomas!现在它给了我这个错误:receive实例没有属性'data',我一直在搜索你的代码。将def printing_handler(addr,tags,data,source)
更改为def printing_handler(self,addr,tags,data,source)
,或使其成为一个静态方法
。此外,将main
函数重命名为\uuuu init\uuuu
,并删除receipt.main()
调用。您实际上从未调用打印处理程序()
函数,因此没有可检索的数据。这就是为什么会出现错误。谢谢…我的问题是打印处理程序()永远不会被调用,因为它在线程中拥有…无论如何,谢谢!!朋友们不允许朋友们在python中使用全局变量:-),尽管在非常小的多线程脚本(如OP)中使用全局变量是不好的做法,它们工作得很好。好的,但是..使用self.handled的类似乎更复杂,我仍然被卡住了:-(
import OSC
import threading
import atexit
#------OSC Server-------------------------------------#
receive_address = '127.0.0.1', 7402
# OSC Server. there are three different types of server.
s = OSC.ThreadingOSCServer(receive_address)
# this registers a 'default' handler (for unmatched messages)
s.addDefaultHandlers()
class receive:
def printing_handler(addr, tags, data, source):
if addr=='/data':
self.data=data.pop(0)
s.addMsgHandler("/data", printing_handler)
return data
def main(self):
# Start OSCServer
#Main function...I need to retrieve 'data' from the OSC THREAD here
print "Starting OSCServer"
st = threading.Thread(target=s.serve_forever)
st.start()
reception=receive()
reception.main()
plouf = data.reception()
print plouf