the power to yield a better console interface

as part of a different project, i needed to duplicate some existing terminal magic in python. what i needed to write was something similar to the getch() function in curses. it can be found in: ncurses*/base/lib_getch.c after doing an: apt-get source libncurses5

what’s the magic? i need to stay in a continuous loop reading from the file descriptor, however i want to return periodically so that gobject doesn’t block and the interface can remain responsive. enter: yield, who comes in and saves the day. see the accompanying code for specifics.

as part of the bigger scheme, i wanted to write a console like interface for talking to a dbus server that both allows you to run methods, and receive signals. i wanted to use gobject, and i didn’t want to use threads! and because i wanted to make it pro, i decided it should look and feel like my standard bash shell (except prettier). it’s intended to be easy to use, and running the module will give you an example session. check back for a more complete and expressive code base.

1) if anyone knows who to do a ualarm (not alarm) in python, help is appreciated. either through ctypes or as a c extension for python.

2) please leave me your comments on my greadline module! more features are on the way, such as history and cursor support.

code: http://www.cs.mcgill.ca/~james/code/greadline.tar

Advertisements

2 thoughts on “the power to yield a better console interface

  1. class threading.Thread

    depending on how you implement it, see join, Condition.wait, Event.wait.

    Best of all: Timer!

    from threading import Timer
    def hello(): print “ding!”
    t = Timer(0.4, hello)
    t.start() # after time time elapses we call the function
    ~
    ~

  2. indeed the threading module is nice!
    the above (and code) is all singly-threaded… i wanted this advantage; makes it easier to reason about a lot of the code, i think it’s the way to go for front end stuff. gobject+select work very well :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s