我有一个UDP服务器侦听线程中的传入流量。消息来自JSON格式的外部设备,例如{“_ id”:“0x00”,“status”:“on”}。这些信息需要由UDP处理程序解析并存储在对象的dict中(如果_id存在则更新)目前我可以接收和解析JSON,但不能确定如何存储这些数据或正确处理它。我想在UDP处理程序中使用一个Queue和一个处理队列的单独的Msg Handler,但不确定它是否是正确的方法。
请注意:我重写了代码,省略了一些语法等。
# I create a node server thread in the main of my program with IP/PORT
class NodeServer(threading.Thread):
def __init__():
self.server = UDPServer((address, port), UDPHandler)
def run():
self.server.serve_forever()
class UDPServer(socketserver.ThreadingUDPServer):
allow_reuse_address = True
class UDPHandler(socketserver.BaseRequestHandler):
data_q = queue.Queue()
handler = NodeHandler(data_q)
handler.start()
def handle():
try:
# receive the message from client
data = self.request[0].decode("UTF-8")
# check if it's in json format
# HANDLE THE MESSAGE
self.data_q.put(data)
# stop the thread
self.handler.join()
# send an "ACK" msg back to the client
except: Exception as e:
#handle
class NodeHandler(threading.Thread):
table = NodeTable()
def __init(data_q):
self.data_q = data_q
def handle():
# get the string message from the queue (filled by the UDPHandler)
msg = self.data_q.get()
# check if the "_id" field exists in the current node table
# if it exists in the table, find it, update the fields from json
# otherwise, if it's a new "_id": create a new node
json_msg = json.parse(msg)
node = Node(json_msg["_id"])
# set other node parameters from the json object, status etc
# update the node table with the new information from the message
table.put(nd)
#
def join():
# join thread
class NodeTable():
# the table is a dictionary with ID and a Node object, ie {0x01: Node}
table = {}
def put(_id, node):
self.table[_id] = node
def get(_id):
return self.table[_id]
class Node():
id = 0
def __init__(id):
self.id = id
# new node
# other node functions
谢谢!
答案 0 :(得分:1)
1)您可以实现处理传入消息的UDP服务器 这些代码行更简单的无限循环:
import socket
def udp_server(udp_ip, udp_port, ...):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((upd_ip, upd_port))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
...process data...
有关详细信息,请参阅https://wiki.python.org/moin/UdpCommunication 和讨论。
udp_server
的参数是udp IP和端口,等等
服务器需要与之交互的其他数据结构。
将其踢到自己的线程中非常容易完成 用:
import threading
t = threading.Thread(target = udp_server, args = (...))
t.start()
2)NodeTable类只是python字典的包装器, 但似乎你想让多个线程访问它 同时。在这种情况下,您应该阅读这个SO答案: (link)
取决于服务器线程之外的其他线程可以做什么 您可能需要也可能不需要锁定的节点字典。
总结一下,我将如何编写代码:
def main():
nodes = {} # use a simple dict for storing the nodes
lock = RLock() # if you need this
# pass nodes and lock to server thread and start it
t = threading.Thread(target = udp_server, args = (udp_ip, udp_port, nodes, lock))
t.start()
...
此时udp服务器正在运行,主线程可以访问该节点
表通过变量nodes
。
添加新节点时是否需要通知主线程
节点表?那么也许一个队列就是你想要的。你会1)创建它
在main()
和2)中将其传递给udp_server
:
def main()
nodes = {} # use a simple dict for storing the nodes
lock = RLock() # if you need this
q = Queue() # create a Queue and pass it to the udp server
# pass nodes and lock to server thread and start it
t = threading.Thread(target = udp_server, args = (udp_ip, udp_port, nodes, lock, q))
t.start()
# process entries from the Queue
while True:
item = q.get()
... process item...
并在udp服务器函数...process data...
中将某些内容放入队列:
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
...json decode, etc. ...
q.put(...)