[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Curses and Threading

Brett.Friermood

1/22/2008 1:06:00 AM

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
4 Answers

Gabriel Genellina

1/22/2008 1:49:00 AM

0

En Mon, 21 Jan 2008 23:06:10 -0200, <Brett.Friermood@gmail.com> escribió:

> 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.

--
Gabriel Genellina

Brett.Friermood

1/22/2008 4:23:00 PM

0

> 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

Ian Clark

1/22/2008 5:24:00 PM

0

On 2008-01-22, Brett.Friermood@gmail.com <Brett.Friermood@gmail.com> wrote:
>> 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

Brett.Friermood

2/3/2008 9:04:00 PM

0

> 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