python - Python grab all UDP Datagrams
问题描述
Server.py ish
import random
import pickle
import time
import socket
num=random.random()
data=pickle.dumps(num)
now=time.time()
mail={'d':data,'t':now}
box=pickle.dumps(mail)
UDP_IP = socket.gethostname()
UDP_PORT = 42096
place=(UDP_IP,UDP_PORT)
tube = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tube.sendto(box, place)
import threading
randomnumstreamgo=True
def randomnumstreamloop(): #This sends a random number to 42069 every 10 seconds
global randomnumstreamgo
while randomnumstreamgo:
num=random.random()
data=pickle.dumps(num)
now=time.time()
mail={'d':data,'t':now}
box=pickle.dumps(mail)
tube.sendto(box, place)
delay=10
time.sleep(delay)
randomnumstream=True
randomnumstreamloophandler=[[thread,thread.start()] for thread in [threading.Thread(target=randomnumstreamloop,args=())]][0]
Client.py ish
import socket
UDP_IP = socket.gethostname()
UDP_PORT = 42096
place=(UDP_IP,UDP_PORT)
tube = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tube.bind(place)
import pickle
def unbox(box):
mail=pickle.loads(box)
data=mail['d']
num=pickle.loads(data)
return num
data, addr = tube.recvfrom(1024)
fresh=0
doit=True
def thefreshest():
data, addr = tube.recvfrom(1024)
global fresh
fresh=unbox(data)
global doit
doit=True
#print(fresh)
thefreshest()
thefreshestgo=True
def thefreshestloop():
global thefreshestgo
while thefreshestgo:
thefreshest()
print("endingfreshestloop")
thefreshestgo=True
import threading
thefreshestloophandler=[[thread,thread.start()] for thread in [threading.Thread(target=thefreshestloop,args=())]][0]
def fibonacci(n):
a = 0
b = 1
if n < 0:
print("Incorrect input")
elif n == 0:
return a
elif n == 1:
return b
else:
for i in range(2,n):
c = a + b
a = b
b = c
return b
import random
def computationallyexpensive(x):
return str(fibonacci(250000+int(x*20000)))[::32][:1024]
grindinggo=False
grindinggo=True
def grindingloop():
global grindinggo
global doit
while grindinggo:
if doit:
print(computationallyexpensive(fresh))
doit=False
print("endinggrindingloop")
grindgo=True
grindinghandler=[[thread,thread.start()] for thread in [threading.Thread(target=grindingloop,args=())]][0]
The way I solve this problem currently is with two threads, one thread constantly updating the variable freshest
and another thread operating on it. Is there a way to grab all the UDP datagrams available, like recvall() with TCP, so I don't need to make a separate thread? I understand that UDP is datagram based and TCP is stream based.
The code is functional, but if there was a way to either 1. grab all the udp packets in one big chunk or 2. grab only the most recent udp packet and throw awa the rest, I could make my code much more neat and efficient.
解决方案
推荐阅读
- flutter - 如何访问地图中的值(跨地图列表)并将这些值转换为列表 FLUTTER
- amazon-web-services - 如何设置 AWS REST API Gateway (v1) 以使用基于路径的路由路由到使用 ECS/Fargate 托管的微服务
- java - 将 Taurus yaml 文件中的变量传递给 Jmeter 脚本以切换 JDBC 连接
- ssis - ole db 适配器使用的 ole db 提供程序无法在 SSIS 中的类型 dt_dbtimestamp 和 dt_numeric 之间转换
- javascript - Discord Bot:使用 Node.js 添加基于 user_id 的角色
- excel - Pilcrow ¶ - CONCATENATE Char(10) 与数字时发生?
- r - R中的堆积面积图
- swift - 在@available 属性中参数化消息
- sass - Sass lightness() 函数不适用于 TailwindCSS 主题变量
- vba - 生成访问令牌导致未授权客户端