motoku motoku - 1 year ago 45
Linux Question

multiple urwid calls getting terminal size leaves files open

If I make the following call:

from urwid import raw_display
cols, rows = raw_display.Screen().get_cols_rows()

... a file descriptor is left open. Making multiple calls to this function crashes the Python 3 Interpreter; especially if done like I was doing previously:

size = lambda rows = True: raw_display.Screen().get_cols_rows()[rows]

How can I prevent the following error?

OSError: [Errno 24] Too many open files

Answer Source

Create raw_display.Screen() only once.

One way to do it is using default paramter; it's evaluated only once - when the function/lambda is defined:

size = lambda rows=True, scr=raw_display.Screen(): scr.get_cols_rows()[rows]