473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Curses programming, threads?

Hello,

I'm writing this little program, and I've came across a function
that I need to call. Since the execution of the function takes a
real long time I want to put a timer, or an animated 'loading'
screen (it would be best if it was a progressbar). The questions is
how to make two commands to run at the same time.

I'm talking about a function which (for example) is connecting to
the database, and while it's connecting I would like to display a
nice loading animation. It could be a ascii sequence like this:
[/] [-] [\] [|] [/] [-] [\] [|]

I could write a function like this:

#v+
def function:
print "[/]"
command
print "[-]"
command
print "[\]"
command
...
#v-

But is this really the way other programs work? I don't think so.
And they don't use threads AFAIK. How can I accomplish something
like this?

It would be best if I could operate with like this:

#v+
def function:
do loader() while
connect_db()
#v-

And the loader() function would run in a loop until connect_db() is
is finished. Is that possible in python? Or are there any other,
better ways to do it?

Thanks a lot.

Regards,

--
Bartek Rymarski <ba**@wsisiz.edu.pl>
$ %blow
bash: fg: %blow: no such job
Jul 18 '05 #1
6 2577
Bartlomiej Rymarski <ba**@oceanic.wsisiz.edu.pl> wrote:
[...]
And the loader() function would run in a loop until connect_db() is
is finished. Is that possible in python? Or are there any other,
better ways to do it?
[...]


Oh, I forgot - I'm using Linux, and curses module in Python 2.3.

--
Bartek Rymarski <ba**@wsisiz.edu.pl>
$ %blow
bash: fg: %blow: no such job
Jul 18 '05 #2
Bartlomiej Rymarski <ba**@oceanic.wsisiz.edu.pl> wrote:
[...]
And the loader() function would run in a loop until connect_db() is
is finished. Is that possible in python? Or are there any other,
better ways to do it?
[...]


Oh, I forgot - I'm using Linux, and curses module in Python 2.3.

--
Bartek Rymarski <ba**@wsisiz.edu.pl>
$ %blow
bash: fg: %blow: no such job
Jul 18 '05 #3
Bartlomiej Rymarski <ba**@oceanic.wsisiz.edu.pl> wrote in message news:<co**********@portraits.wsisiz.edu.pl>...
Bartlomiej Rymarski <ba**@oceanic.wsisiz.edu.pl> wrote:
[...]
And the loader() function would run in a loop until connect_db() is
is finished. Is that possible in python? Or are there any other,
better ways to do it?
[...]


Oh, I forgot - I'm using Linux, and curses module in Python 2.3.


You don't need curses. Some time ago somebody (I forgot the name)
posted this spinner class:

class Spinner( threading.Thread ): # a google search should find the author

DELAY = 0.1
DISPLAY = [ '|', '/', '-', '\\' ]

def __init__( self, before='', after='' ):
threading.Thread.__init__( self )
self.before = before
self.after = after

def run( self ):
write, flush = sys.stdout.write, sys.stdout.flush
self.running = 1
pos = -1
while self.running:
pos = (pos + 1) % len(self.DISPLAY)
msg = self.before + self.DISPLAY[pos] + self.after
write( msg )
flush()
write( '\x08' * len(msg) )
time.sleep( self.DELAY )
write( ' ' * len(msg) + '\x08' * len(msg) )
flush()

def stop( self ):
self.running = 0
self.join()

if __name__=="__main__":
spinner = Spinner('Be patient please ...')
spinner.start()
time.sleep(5) # doing a long operation
spinner.stop()

Michele Simionato
Jul 18 '05 #4
Bartlomiej Rymarski <ba**@oceanic.wsisiz.edu.pl> wrote in message news:<co**********@portraits.wsisiz.edu.pl>...
Hello,

I'm writing this little program, and I've came across a function
that I need to call. Since the execution of the function takes a
real long time I want to put a timer, or an animated 'loading'
screen (it would be best if it was a progressbar). The questions is
how to make two commands to run at the same time.
[snip]
It would be best if I could operate with like this:

#v+
def function:
do loader() while
connect_db()
#v-

And the loader() function would run in a loop until connect_db() is
is finished. Is that possible in python?


Generally speaking, to do this in C, on Unix, without threads, one
would use the setitimer system call, along with SIGALARM. (Doubtful
available on all Unices, and there are probably other ways, but I'd
guess this is the most common way to do it. It is available on Linux;
I've used it before.) AFAIK, Python does not expose the setitimer
call, so you can't do it that way in Python without writing a C
extension. (It would be a pretty simple extension to write, though.)

In Python, you could use the signal.alarm() call in much the same way.
The downside is that you can only update the little animation once
per second. Something like this could do what you want (untested):

def alarm_handler(*args):
if loaded:
return
update_animation()
signal.alarm(1)

signal.signal(signal.SIGALARM,alarm_handler)
signal.alarm(1)
loaded = False
connect_db()
loaded = True

But I recommend threads for this. It's one of the easiest possible
uses of threads. There's no complex communication involved; locks and
semaphores and stuff aren't required. Just connect to the database in
a subthread, and have it set a global flag just before it exits.
Animate in a loop in the main thread, checking the flag every
iteration, and when it's true, you're done.
--
CARL BANKS
Jul 18 '05 #5
Michele Simionato <mi***************@gmail.com> wrote:
You don't need curses. Some time ago somebody (I forgot the name)
posted this spinner class:

class Spinner( threading.Thread ): # a google search should find the author
[...]


Great, thanks a lot.

--
Bartek Rymarski <ba**@wsisiz.edu.pl>
$ %blow
bash: fg: %blow: no such job
Jul 18 '05 #6
Carl Banks <im*****@aerojockey.com> wrote:
(...)
But I recommend threads for this. It's one of the easiest possible
uses of threads. There's no complex communication involved; locks and
semaphores and stuff aren't required. Just connect to the database in
a subthread, and have it set a global flag just before it exits.
Animate in a loop in the main thread, checking the flag every
iteration, and when it's true, you're done.
(...)


I'll try to do this with threads then. Thanks for the answer.

--
Bartek Rymarski <ba**@wsisiz.edu.pl>
$ %blow
bash: fg: %blow: no such job
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: AK | last post by:
Hi, I have a medium sized app that I'm transferring to use curses. What happens is very very strange.. I am at my wits' ends. I worked with it all day yesterday and couldn't figure it out....
2
by: Konrad Koller | last post by:
import curses produces the ImportError: No module named _curses ("from _curses import *" in line 15 in __init__.py) Of course imp.find_module ("_curses") reports the same error. How can I make...
3
by: harold fellermann | last post by:
Hi all, I want to use curses in a server application that provides a GUI for telnet clients. Therefore, I need the functionality to open and handle several screens. Concerning...
7
by: M.Senthil Kumar | last post by:
hai all, I need a help from you. I 'm working in a project using "curses.h" in Linux using 'C'. I have some doughts regarding menus and line. 1. I used to draw box in window using...
10
by: Michael | last post by:
I am having a hard time trying to find out how to stop windows from line wrapping using the curses library. Does anyone know how? Thanks, Michael
1
by: Jerry Fleming | last post by:
Hi, I have wrote a game with python curses. The problem is that I want to confirm before quitting, while my implementation doesn't seem to work. Anyone can help me? #!/usr/bin/python # #...
48
by: Daniele C. | last post by:
As soon as my sourceforge.net project gets approved, I am going to build a ncurses port to win32 bindable to sockets, e.g. allowing VT100/ANSI terminals and the creation of simple terminal servers...
15
by: pinkfloydhomer | last post by:
I need to develop a cross-platform text-mode application. I would like to do it in Python and I would like to use a mature text-mode library for the UI stuff. The obvious choice, I thought, was...
5
by: 7stud | last post by:
I can't see to get any y, x coordinates to work with curses. Here is an example: import curses def my_program(screen): while True: ch = screen.getch() if ch == ord("q"): break
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.