python - How select.select() works?

后端 未结 2 543
执笔经年
执笔经年 2021-01-30 13:50

Background:

I\'m familiar with C\'s select() function. I\'ve been using this function for many purposes. Most of them, if not all, for reading and writing

相关标签:
2条回答
  • 2021-01-30 14:30

    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.

    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.

    Suggestions for you to try:

    • 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.
    0 讨论(0)
  • 2021-01-30 14:47

    Can I ask a stupid question - are you sure there are really 8 bytes left?

    Device nodes don't necessarily behave like normal files. It might be that you have to read the entire struct input_event in a single read() system call. (And if you don't read enough, the rest gets thrown away). A bit like recvmsg() on datagram sockets.

    0 讨论(0)
提交回复
热议问题