在 Python 中操作 select.select 的文件描述符

发布于 2024-09-28 03:41:39 字数 686 浏览 2 评论 0原文

我有一个瘙痒问题,我知道可以使用许多不同的方法来解决,但我仍然想知道以下方法在 Python 中是否可行。

假设我有一些套接字,我一直在等待输入,并且有一些条件最终终止整个程序。正如我想象的那样,我想使用 select.select 以阻塞方式执行此操作:

readfds, writefds, errfds = select.select([mysocket],[],[])
if readfds:
    conn, addr = mysocket.accept()
    ...

现在,如果有某个文件描述符 fd,我可以手动将其设置为就绪条件,无论是读还是写,我都可以做

readfds, writefds, errfds = select.select([mysocket,fd],[],[])
for r in readfds:
    if r == mysocket:
        conn, addr = mysocket.accept()
        ...
    else:
        <terminate>

当然,我可以简单地向 mysocket 发送一条消息,使其解除阻塞,但我仍然想知道是否有一种编程方法可以将文件描述符操纵到就绪状态。

编辑:我的问题是:我可以以某种方式将文件描述符手动设置为“就绪”吗?

谢谢大家。

I have an itching problem I know could be solved using many different ways, but I would still like to know if the following approach is possible in Python.

Suppose I have some socket I am constantly waiting for input on, and there is some condition that eventually terminates the whole program. I want to do it in a BLOCKING fashion, as I imagined, using select.select:

readfds, writefds, errfds = select.select([mysocket],[],[])
if readfds:
    conn, addr = mysocket.accept()
    ...

Now, if there is some file descriptor fd, that I can manually set to a ready condition, either read or write, I can do

readfds, writefds, errfds = select.select([mysocket,fd],[],[])
for r in readfds:
    if r == mysocket:
        conn, addr = mysocket.accept()
        ...
    else:
        <terminate>

Of course, I can simply send a message to mysocket, causing it to unblock but I would still like to know if there is a programmatic way to manipulate a file descriptor to a ready state.

EDIT: My question is: can I somehow set a file descriptor to "ready" manually?

Thanks all.

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

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

发布评论

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

评论(3

∞觅青森が 2024-10-05 03:41:39

最简单的方法可能是使用 os.mkfifo()创建文件对,将读端添加到 select() 调用中,然后在要解除阻塞时写入到写端。

另外,您可能需要考虑为 select() 调用添加超时;我无法想象您在畅通的时间内会做足够多的事情来拖累性能。

The easiest thing to do is probably to use os.mkfifo() to create a file pair, add the read end to the select() call, and then write to the write end when you want to unblock.

Also, you might want to consider just adding a timeout to the select() call; I can't imagine that you'd be doing enough during the unblocked time to drag performance down.

柏林苍穹下 2024-10-05 03:41:39

我认为您必须创建一个套接字对(请参阅 socket.socketpair() 函数)并生成一个单独的 Python 线程(使用 threading.Thread 类)来注意告诉程序何时结束的特殊条件。当线程检测到条件时,它可以写入“完成!” (或其他)连接到套接字对的一端。如果您正在等待读取的套接字列表中有套接字对的另一端,那么它会在“完成!”后亮起并表示它是可读的。出现并准备好从套接字读取。

I think that you will have to create a socket pair (see the socket.socketpair() function) and spawn a separate Python thread (use the threading.Thread class) to watch for the special condition that tells your program when to end. When the thread detects the condition, it can write "Done!" (or whatever) on to its end of the socket pair. If you have the other end of the socket pair in the list of sockets you are waiting on for reading, then it will light up and say that it is readable as soon as the "Done!" appears and is ready to be read off of the socket.

╰つ倒转 2024-10-05 03:41:39

使用未命名管道 os.pipe() 也可以很好地工作这里(在读取端阻塞,写入写入端)并使您不必指定路径。

当我希望一个线程能够中断另一个线程在选择调用上的阻塞时,我发现这种设置对于多个线程很有用。

Using an unnamed pipe os.pipe() would also work well here (block on the read end, write to the write end) and keep you from having to specify a path.

I've found this sort of setup useful with multiple threads, when I want one thread to be able to interrupt another thread's blocking on a select call.

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