Curses and Threading

B

Brett.Friermood

I am writing a program that uses curses and threading. I am working on
displaying a clock in the upper right hand corner of the screen. I
have only one thread at the moment, that gets the time and displays it
in curses. To make it easier to debug right now, the program starts
curses in a try: clause then starts the thread which runs for 10
iterations displaying the time every second. Then the thread exits,
and the program is supposed to run the finally: clause to clean up the
terminal. I also have set curs_set(0) so the cursor is invisible. What
happens is that everything starts fine but the cursor is visible. It
runs for the 10 seconds then quits without restoring the terminal to
working order. I am trying this on a Fedora 4 computer have also tried
it on a Fedora 8 one with same results. I have tried searching but
Google doesn't find anything using both curses and threading. The only
thing I can find regarding both is that curses seems to block when
waiting for input, but I do not have any input yet. Below is what I
have right now:

#! /usr/bin/env python
import curses
import threading
import time
class cadtime(threading.Thread):
def run(self):
x=0
while x<10:
cadtimevl=time.strftime("%d %b %H:%M:
%S",time.localtime())
leng=len(cadtimevl)
stdscr.addstr(0,width-leng-1,cadtimevl)
stdscr.refresh()
x=x+1
time.sleep(1)
return
try:
stdscr=curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
curses.start_color()
curses.curs_set(0)
width=curses.COLS-1

cadtime().start()

finally:
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()

I can't figure out why the cursor still shows and why they terminal is
screwed up afterward because the finally: should catch any failures
and reset the terminal.

-Brett
 
G

Gabriel Genellina

I am writing a program that uses curses and threading. I am working on
displaying a clock in the upper right hand corner of the screen. I
have only one thread at the moment, that gets the time and displays it
in curses.

In fact you have *two* threads: the main thread, and the one you create
explicitely.
To make it easier to debug right now, the program starts
curses in a try: clause then starts the thread which runs for 10
iterations displaying the time every second. Then the thread exits,
and the program is supposed to run the finally: clause to clean up the
terminal.

After you start the clock thread, the main thread continues executing,
immediately entering the finally clause.
If you want to wait for the other thread to finish, use the join() method.
But I'm unsure if this is the right way to mix threads and curses.
 
B

Brett.Friermood

In fact you have *two* threads: the main thread, and the one you create
explicitly.
After you start the clock thread, the main thread continues executing,
immediately entering the finally clause.
If you want to wait for the other thread to finish, use the join() method.
But I'm unsure if this is the right way to mix threads and curses.

This is what the python documentation says:

join([timeout])
Wait until the thread terminates. This blocks the calling thread
until the thread whose join() method is called terminates.

So according to this since I need to block the main thread until the
clock thread ends I would need the main thread to call
"cadtime().join()", correct? I'm not sure how to do this because I
don't have a class or anything for the main thread that I know of. I
tried putting that after cadtime().start() but that doesn't work. I
guess what I'm trying to say is how can I tell the main thread what to
do when it doesn't exist in my code?

Thanks for the help
-Brett
 
I

Ian Clark

In fact you have *two* threads: the main thread, and the one you create
explicitly.
After you start the clock thread, the main thread continues executing,
immediately entering the finally clause.
If you want to wait for the other thread to finish, use the join() method.
But I'm unsure if this is the right way to mix threads and curses.

This is what the python documentation says:

join([timeout])
Wait until the thread terminates. This blocks the calling thread
until the thread whose join() method is called terminates.

So according to this since I need to block the main thread until the
clock thread ends I would need the main thread to call
"cadtime().join()", correct? I'm not sure how to do this because I
don't have a class or anything for the main thread that I know of. I
tried putting that after cadtime().start() but that doesn't work. I
guess what I'm trying to say is how can I tell the main thread what to
do when it doesn't exist in my code?

Thanks for the help
-Brett

join() is a method on Thread objects. So you'll need a reference to the
Thread you create, then call join() on that.

thread = cadtime()
thread.start()
thread.join()

Ian
 
B

Brett.Friermood

join() is a method on Thread objects. So you'll need a reference to the
Thread you create, then call join() on that.

thread = cadtime()
thread.start()
thread.join()

Ian

Thanks for all the help guys. It's working great. Just one more
question, I think. As you probably guessed, I want to have a second
and possibly third thread. I tried calling it in different ways, but
if the first one is joined the second one will not run. But if the
first one is not joined curses exits early like before. If I can
figure out how to use multiple threads with curses now I think I will
be set.

-Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top