python-select.select()如何工作? [英] python - How select.select() works?

查看:115
本文介绍了python-select.select()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉select() C函数.我已经将此功能用于许多用途.它们中的大多数(如果不是全部的话)用于读取和写入管道,文件等...我必须说,我从未使用过错误列表,但这并不涉及关键问题.

I'm familiar with select() C function. I've been using this function for many purpouses. Most of them, if not all, for reading and writting to pipes, files, etc... I must say that I've never used the error list, but this is not involved in the key question.

python select()是否表现如下?

Does python select() behaves as the following?

在我看来,尽管 直截了当,但python上的select()的行为方式却有所不同 接口连接到C select().似乎select()会在文件准备好首次读取时返回.如果您在队列中读取文件以字节为单位,则调用select()将阻塞.但是,如果返回上一个对select()的调用后又再次调用select(),而在这两个调用之间没有任何读取调用,则select()将按预期返回.例如:

It turns out to me that select() on python behaves a different way despite the straightforward interface to C select(). It seems that select() returns the very first time a file is ready for reading. If you read the file letting being bytes on its queue, calling select() will block. But, if you call select() again after a previous call to select() was returned without any read call between these two calls, select() will return as expected. For example:

import select
# Open the file (yes, playing around with joysticks)
file = open('/dev/input/js0', 'r') 
# Hold on the select() function waiting
select.select([file], [], [])
# Say 16 bytes are sent to the file, select() will return.
([<open file '/dev/input/js0', mode 'r' at 0x7ff2949c96f0>], [], [])
# Call select() again, and select() will indeed return.
select.select([file], [], [])
([<open file '/dev/input/js0', mode 'r' at 0x7ff2949c96f0>], [], [])
# read 8 bytes. There are 8 bytes left for sure. Calling again file.read(8) will empty the queue and would be pointless for this example
file.read(8)
'<\t\x06\x01\x00\x00\x81\x01'
# call select() again, and select() will block
select.select([file], [], [])
# Should it block? there are 8 bytes on the file to be read.

如果这是python中select()的行为,那么我可以,我可以处理.虽然不是我期望的那样,但是很好,我知道我能用它做什么.

If this is the behaviour of select() in python, I'm okay with that, I could handle it. Not what I expected though, but it's fine, I know what I can do with it.

但是,如果这不是select()的行为,我将不胜感激有人告诉我我做错了什么.我读到的关于select()的内容是python doc所说的:如果read | error |列表中的任何文件已准备好进行read | write | error,则select()返回.".没关系,那里没有谎言.也许问题应该是:

But, if this is not the behaviour of select() I would appreciate someone to tell me what I'm doing wrong. What I read about select() is what the python doc says: "select() returns if any file in the read|write|error list is ready for read|write|error.". That's ok, no lies there. Maybe the questions should be:

  • 何时认为文件已准备就绪,可以在python中读取?
  • 这是否意味着从未读取过的文件?
  • 这是否意味着要读取字节的文件?

推荐答案

Python的select()作为select()系统调用被传递,正如您所期望的那样,但是阻塞的问题是一个不同的问题,可能是与缓冲有关.只是为了使自己确信select()在做正确的事情,请尝试在文件系统上读取/写入文件,而不要使用操纵杆之类的特殊设备.

Python's select() gets passed through as a select() system call as you are expecting, but the problem you have with it blocking is a different issue, probably relating to buffering. Just to satify yourself that select() is doing the right thing, try reading/writing a file on the file system rather than using a special device such as a joystick.

您可能想更改您的open()通话. Python open 调用默认情况下将使用缓冲读取,因此即使您这样做read(8)可能会从输入文件中读取更多数据并缓冲结果.您需要将buffering选项设置为open,以便可以不带缓冲地打开操纵杆设备.

You probably want to change your open() call. Pythons open call will by default use buffered reads, so even if you do a read(8) it will likely read more data from the input file and buffer the results. You need to set the buffering option to open so that the joystick device is opened unbuffered.

建议您尝试:

  • Python默认以文本模式打开文件.处理诸如操纵杆之类的特殊设备时,您可能希望打开的moderb.
  • 以无缓冲模式打开文件.
  • 如果要进行基于select的呼叫,请将设备设置为非阻塞模式.尝试将os.open()os.O_RDONLY|os.O_NONBLOCK标志一起使用.
  • Python defaults to opening files in text mode. You probably want the open mode to be rb when dealing with special devices such as a joystick.
  • Open file in unbuffered mode.
  • Set the device into non-blocking mode if you're going to be doing select based calls. Try using os.open() with os.O_RDONLY|os.O_NONBLOCK flags.

这篇关于python-select.select()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆