socket.error errno.EWOULDBLOCK

发布于 2024-09-17 06:17:47 字数 113 浏览 5 评论 0原文

我正在阅读一些代码,并且遇到了这一行,

socket.error errno.EWOULDBLOCK

任何人都可以告诉我引发此错误的条件是什么?

i'm reading some code and i've come across this line

socket.error errno.EWOULDBLOCK

can anyone tell me what the conditions have to be to raise this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芸娘子的小脾气 2024-09-24 06:17:48

来自Python的套接字模块: http://docs.python.org/library/socket.html

最初所有套接字都处于阻塞状态
模式。在非阻塞模式下,如果
recv() 调用未找到任何数据,或者
如果 send() 调用不能立即执行
处理数据,错误异常
被提出

它引用的错误异常是 errno.EWOULDBLOCK

要发生这种情况,必须使用以下方法将套接字对象设置为非阻塞模式: socketObj.setblocking(0)

From Python's socket module: http://docs.python.org/library/socket.html

Initially all sockets are in blocking
mode. In non-blocking mode, if a
recv() call doesn’t find any data, or
if a send() call can’t immediately
dispose of the data, a error exception
is raised
.

The error exception it's referring to is errno.EWOULDBLOCK

For this to happen, the socket object must be set to non-blocking mode using: socketObj.setblocking(0)

苄①跕圉湢 2024-09-24 06:17:48

请注意,EWOULDBLOCK 的错误号为 11:

In [80]: import errno
In [83]: errno.EWOULDBLOCK
Out[84]: 11

相关的错误消息为:

In [86]: import os
In [87]: os.strerror(errno.EWOULDBLOCK)
Out[89]: 'Resource temporarily unavailable'

这是一些显示 EWOULDBLOCK 错误的玩具代码。
它设置一个服务器和客户端,尝试通过套接字连接相互通信。当调用 s.setblocking(0) 将套接字置于非阻塞模式时,对 s.recv 的后续调用将引发 socket.error.我认为发生这种情况是因为连接的两端都试图接收数据:

import socket
import multiprocessing as mp
import sys
import time

def server():
    HOST='localhost'
    PORT=6000
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr=s.accept()
    while True:
        data=conn.recv(1024)
        if data:
            conn.send(data)
    conn.close()   
def client():
    HOST='localhost'
    PORT=6000
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.setblocking(0)     # Comment this out, and the EWOULDBLOCK error goes away
    s.send('Hello, world')
    try:
        data=s.recv(1024)
    except socket.error as err:
        print(err)
        # [Errno 11] Resource temporarily unavailable
        sys.exit()
    finally:
        s.close()
    print('Received {0}'.format(repr(data)))
def run():
    server_process=mp.Process(target=server)
    server_process.daemon=True
    server_process.start()
    time.sleep(0.1)
    client()   
run()

如果 s.setblocking(0) 被注释掉,您应该看到

Received 'Hello, world'

Note that EWOULDBLOCK is error number 11:

In [80]: import errno
In [83]: errno.EWOULDBLOCK
Out[84]: 11

And the associated error message is:

In [86]: import os
In [87]: os.strerror(errno.EWOULDBLOCK)
Out[89]: 'Resource temporarily unavailable'

Here is some toy code which exhibits the EWOULDBLOCK error.
It sets up a server and client which try to talk to each other over a socket connection. When s.setblocking(0) is called to put the socket in non-blocking mode, a subsequent call to s.recv raises the socket.error. I think this happens because both ends of the connection are trying to receive data:

import socket
import multiprocessing as mp
import sys
import time

def server():
    HOST='localhost'
    PORT=6000
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr=s.accept()
    while True:
        data=conn.recv(1024)
        if data:
            conn.send(data)
    conn.close()   
def client():
    HOST='localhost'
    PORT=6000
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.setblocking(0)     # Comment this out, and the EWOULDBLOCK error goes away
    s.send('Hello, world')
    try:
        data=s.recv(1024)
    except socket.error as err:
        print(err)
        # [Errno 11] Resource temporarily unavailable
        sys.exit()
    finally:
        s.close()
    print('Received {0}'.format(repr(data)))
def run():
    server_process=mp.Process(target=server)
    server_process.daemon=True
    server_process.start()
    time.sleep(0.1)
    client()   
run()

If s.setblocking(0) is commented-out, you should see

Received 'Hello, world'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文