- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这个问题确实集中在我的问题上,与我在该主题上找到的任何其他问题无关。
PSA:当我说“数据包”时,我的意思是在单个 socket.recv(maxsize)
中接收到的完整字符串我在 Java(我的首选语言)上开发了类似的代码以获得相同的结果,没关系,现在我必须在 python 中做。
我有两个并行运行的进程:1-连接到特定IP的普通客户端套接字2-绑定(bind)到“所有”IP 的“客户端”数据报套接字。
正常套接字按我的预期正常工作,而数据报则不然。
我持续以每秒超过 5 个的速度从服务器(不是我的也不是开源的)接收数据包,但我只想每 3 秒处理其中一个。在java中,我只做了一个“ sleep ”,没关系,我只得到最后一个实时数据包,而在Python中,使用“time.sleep(3)”数据包排队(我不知道如何以及在哪里)并且没有被丢弃。
我必须删除它们,因为这些是不需要的,而且我必须在一个和另一个之间进行 HTTP 调用,这样我就无法为以该速率接收到的每组数据触发 HTTP post!
这是我的监听套接字的“代码”,一些注释是针对私有(private)代码的:
def listenPositions():
lsSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
lsSocket.bind(("0.0.0.0", 8787))
lsSocket.setblocking(0)
try:
while True:
ready = select.select([lsSocket], [], [], 1)
if ready[0]:
lsSocket.settimeout(1)
recvData = lsSocket.recv(16384)
if len(recvData) != 0:
recv = recvData[0:len(recvData)].decode("utf-8")
#print("LS: Received: " + recv)
strings = filter(None, str(recv).split('\n'))
print("Strings count=" + str(len(strings))+ ": " + str(strings))
for item in strings:
#parse the received strings as json and get the items
jsonPosition = json.loads(item)
strId = jsonPosition["id"]
coordinates = jsonPosition.get("coordinates")
if coordinates is None:
continue
print("coordinates not null:" + str(coordinates))
#DO THE HTTP POST REQUEST
time.sleep(3) #Pause the system for X seconds, but other packets are queued!
else:
print("LS: Received empty")
else:
print("LS: No data, timeout")
except Exception as e:
print(e)
#handle exceptions...
print("Exception, close everything")
最佳答案
当您有一个打开的套接字时,所有正确寻址的数据包都应传递到应用程序。我们希望我们的网络连接尽可能可靠,不是吗?丢弃数据包是最后的手段。
如果您只想偶尔获取数据包,您可以创建一个监听套接字,获取数据包并关闭套接字。
然而,没有什么比忽略数据包更容易的了。只需跳过其处理并继续。下面的代码不完整,但希望表达了我的意思。
TIMEOUT = 1.0
INT = 3.0 # interval in seconds
# create udp_socket
last = time.time() - INT
udp_socket.settimeout(TIMEOUT)
while True:
try:
packet = udp_socket.recv(MAXSIZE)
except socket.timeout:
# handle recv timeout
continue # or break, or return
except OSError:
# handle recv error (Python 3.3+)
break # or continue, or return
now = time.time()
if now - last >= INT:
# process the packet
last = now
请注意,如果您仅从一个来源读取内容,则不需要 select
。
关于python - 丢弃数据报套接字的传入 'packets',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669991/
(>>)的定义功能如下:(>>) :: Monad m => m a -> m b -> m b但我想实现这个功能翻转如下: 我有一个函数tabulate :: Int -> [Int] -> IO
我在新线程中创建一个新的托管对象上下文,并向其中插入一些对象。我可以通过不保存上下文来丢弃(忘记它们)它们吗?我的问题是这样的:我开始一个漫长的过程,在开始时创建一些 NSManagedObjects
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 3年前关闭。 Improve t
虽然建议打开编译器标志,如 -Wvalue-discard或 -Wunused:implicits通过使用 sbt-tpolecat 显式或隐式地. 有时您需要解决这些问题,但要以一种明确的方式;因为
在 R 中,na.omit()函数可用于丢弃包含 NA 值的 data.frame 中的条目。作为副作用,如果确实丢弃了行,则该函数会向结果添加一个属性 'omit',其中包含被丢弃的 row.nam
使用 geom_blank我想添加一些新的因子水平,但我似乎无法这样做并保留 NA水平 library('ggplot2') pl <- ggplot(data.frame(x = factor(c(
谁能告诉我两者之间是否有什么区别 DROP IF EXISTS [TABLE_NAME] DROP [TABLE_NAME] 我问这个问题是因为我在 MVC Web 应用程序中使用 JDBC 模板。如
例如,我有以下表达式映射: function! DelNthSpace() let col=match(getline('.'), '[[:blank:] ]', col('.'
默认情况下,Chrome 会丢弃后台未使用的选项卡以节省内存。 我想知道是否有一种方法可以让我现在可以在我的网站被 Chrome 丢弃的情况下 - 这样我就可以告诉我的用户,下次他们进入时,如何避免这
我的数据库中有一个元素列表,每个元素都有自己的日期时间字段。我想丢弃比今天更旧的元素,这意味着我只会看到 future 的元素。 这是我当前的代码: def get_dates_approved(re
我为我的 linux 机器编写了一个 C++ 工具,它接收 UDP (OSC) 数据包并立即将它们发回(这是它唯一做的事情)。但似乎有一些丢弃的数据包。当我向我的 linux(从另一台机器)发送 10
在创建 cron 作业时,我总是这样写: 15 0 * * * /somepath/somescript.pl > /dev/null 2>&1 关于末尾“>/dev/null 2>&1”丢弃所有st
我已经四处寻找有关此问题的指示,但找不到任何指示。假设我有一个 Python 列表: list = set([((3, 2), (2, 1)), ((3, 2), (3, 1)
我正在介绍 C,我需要编写一个程序来提示用户输入字符、等号和整数。我需要使用 getchar() 直到 '=' 然后使用 scanf() 获取整数。然后程序应该只输出整数返回给用户。 现在它打印出不必
如何忽略数据框中随机分布在数据框中的空单元格?这是我的例子 ColA ColB ColC ColD ColF ColG ColH A
我无法在网上找到我的问题的单一答案... 我正在为我的投资组合构思一个网站创意,并决定涉足一些 jQuery 让它更令人兴奋。我正确链接了 jQuery 并且工作,但是当我使用 .toggle 方法时
据我了解,在 Linux 中创建虚拟驱动器的过程是这样的 fallocate -l size /path/to/file.img; dd if=/dev/zero of=/path/tofile.im
大多数时候,我都是这样做的。 class a { public: ~ a() { i = 100; // OK delete (int *)j
下面的代码编译。它似乎运行良好。 但是它会导致任何未定义的行为吗? 我想抛弃 *this 的常量。 这是为了允许 const my_iterator 改变它指向的数据。 测试: class A { p
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Is const_cast safe? 显然我永远不会写这段代码,但它是真实程序中出现的一个非常简单的例子。
我是一名优秀的程序员,十分优秀!