Skip to content

Polling standard input with selectors #7828

@showermat

Description

@showermat

I need to wait for a keypress or signal, and thought the easiest way to do this would be to use poll and friends. In the simplified example below, the program responds properly when ^C is pressed, but an assertion not (pkey.ident == 0) fails when input is provided on standard input. It looks like the selectors module uses 0 to mean that the slot is empty, when 0 also happens to be the file descriptor of stdin. Is this intentional? Is there a workaround or better way to achieve this?

import selectors, posix, os
var events = newSelector[int]()
events.registerHandle(0, {Read}, 0)
events.registerSignal(2, 0)
for event in events.select(-1): echo($event & "\n")

(As accessory questions, where is it documented that I need to import posix and os in order to use selectors (see #7667), and what type should I replace int with in the example above to indicate that I don't want to pass any application-defined data at all?)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions