gpt4 book ai didi

python-asyncio - 获取 OSError : (Address already in use) while runnning a function that uses trio-sockets in a while loop

转载 作者:行者123 更新时间:2023-12-02 02:39:38 26 4
gpt4 key购买 nike

代码

import trio
from trio import socket

async def listen(host, port):
while True:
fullmsg = ""
sock = socket.socket()
await sock.bind((host, port))
sock.listen()
print(f'Awaiting Receive On {host}:{port}')
conn, addr = await sock.accept()
print(f'Connection Received From {addr[0]}:{addr[1]}')
while True:
try:
msg = await conn.recv(8)
if len(msg.decode().strip()) > 0:
print(f'Received {len(msg.strip())} bytes')
fullmsg += msg.decode().strip()
else:
break
except Exception as e:
print(f'DEBUG: {e}')
sock.shutdown(0)
sock.close()
print(fullmsg)


# function that runs the listen function:
async def create():
async with trio.open_nursery() as nursery:
nursery.start_soon(listen, '127.0.0.1', 6969)


# To run the program
trio.run(create)

我想在每次收到长度为 0 的消息或连接被客户端关闭时一遍又一遍地运行该函数,但是当该函数完成第一个 while 循环的第一次迭代时,它会给出一个 OSError 说端口已经正在使用。我在循环结束时关闭并关闭了我的套接字,但我仍然不知道程序在哪里出错。

程序的输出
Awaiting Receive On 127.0.0.1:6969
Connection Received From 127.0.0.1:37122
Received 8 bytes
Received 5 bytes
Hello, World!
Traceback (most recent call last):
File "./ape.py", line 68, in <module>
trio.run(create)
File "/usr/local/lib/python3.8/dist-packages/trio/_core/_run.py", line 1804, in run
raise runner.main_task_outcome.error
File "./ape.py", line 59, in create
nursery.start_soon(listen, '127.0.0.1', 6969)
File "/usr/local/lib/python3.8/dist-packages/trio/_core/_run.py", line 730, in __aexit__
raise combined_error_from_nursery
File "./ape.py", line 15, in listen
await sock.bind((host, port))
File "/usr/local/lib/python3.8/dist-packages/trio/_socket.py", line 473, in bind
return self._sock.bind(address)
OSError: [Errno 98] Address already in use

最佳答案

就像其他人在评论中所说的那样,问题是在 Unix-y 平台上,您必须设置 SO_REUSEADDR如果您希望能够关闭监听套接字,然后立即打开一个绑定(bind)到同一端口的新套接字,请使用套接字选项。

但请注意,在 Windows 上,您永远不应该设置 SO_REUSEADDR选项,因为在 Windows 上,您想要的行为默认启用,SO_REUSEADDR被重新定义为“关闭安全”选项。
trio.socket非常底层并且暴露了所有这些细节,所以如果你想自己处理它们,它可以让你这样做。但大多数用户最好使用更高级的助手,如 trio.serve_tcp。 ,它将自动处理很多这些细节。

关于python-asyncio - 获取 OSError : (Address already in use) while runnning a function that uses trio-sockets in a while loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60616038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com