Wutaz
Wutaz

Reputation: 372

Do something while waiting for a blocking action

I'm trying to write a curses program in Lua (mostly for ease of development before porting it to C). It needs to be constantly ready for the user to send commands, but I also want it to be able to act on other signals, like the expiration of timers. Example uses for this might be blinking indicators, nagging the user if they take too long, and printing messages recieved over a network.

When I call curses.getch(), the program waits for it to return data before running the next instruction. If any other event occurs before a key is pressed, the script will not be able to react to it unless something else is done to change that.

I tried using curses.halfdelay() to make curses.getch() exit after a short period of time so that something else could be done before calling it again, but it caused strange errors. I also considered learning to use pthreads, but I don't know where to begin. I am running Linux, and I don't mind if the solution doesn't work on Windows (since I'm already using curses).

EDIT: I might have mislead people by providing so many curses-related details, but I would really prefer a more general solution that can work with other blocking operations.

Upvotes: 2

Views: 222

Answers (1)

William McBrine
William McBrine

Reputation: 2266

halfdelay() and (more commonly) nodelay() are the correct solutions, in the curses space. I can assure you that they do work in the general case. You'll have to be more specific about your "strange errors".

Meanwhile, although it's not a pure curses solution (and won't work with PDCurses), people sometimes use select() in this situation, to wait on input from either curses or a network socket.

Upvotes: 2

Related Questions