pygtk and threading

K

kingcrowbar.list

Hello Everyone

I have been playing a little with pyGTK and threading to come up with
simple alert dialog which plays a sound in the background. The need for

threading came when in the first version i made, the gui would freeze
after clicking the close button until pygame finished playing the
sound.
In Windows it was acceptable because it could be ignored easily, but in

testing on linux (red hat 9) Gnome was throwing up a dialog for killing

hanging applications.

So now i have a threaded version that seems to work as it should
(except not
on rh9 beause gtk is to old, but that is getting upgraded to fc5/6 so
no
matter). So i was hoping to get some general comments about the code as
i am
not sure if i am doing the gtk or threading correctly (i hope the
pygame part
is simple enough that i have it right).


##code start##
#!/usr/bin/env python
"""
Simple alert message dialog with sound.
Runs a dummy audio function if pygame is missing and not run on
Windows.
It does work on linux (fc5) but i don't know the name of any wav file
so
i just copy the Windows tada.wav into same directory as script.
"""

import sys
import threading
import time
import gobject
import gtk
import os.path
try:
import pygame.mixer as pgmixer
import pygame.time as pgtime
except ImportError:
print "pygame not found, using dummy audio"

gobject.threads_init()

alertmsg = "Ding Dong"

class ThreadOne(threading.Thread):
"""Dialog thread"""
stopthread = threading.Event()
def __init__(self):
super(ThreadOne, self).__init__()
self.dialog = gtk.MessageDialog(parent=None,
type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_NONE, flags=gtk.DIALOG_MODAL,
message_format=alertmsg)
self.quit = False

def run(self):
"""Run idle sleep loop while stopthread Event is not set"""
while not self.stopthread.isSet():
print self, " is running"
time.sleep(1)

def stop(self):
"""Set stopthread Event"""
self.stopthread.set()

class ThreadTwo(threading.Thread):
"""Audio thread"""
stopthread = threading.Event()
def __init__(self):
super(ThreadTwo, self).__init__()
self.quit = False
self.cnt = 0
self.killaudio = 0

def run(self):
"""While audioalert returns True, increment counter"""
if (sys.modules.has_key('pygame')) and (sys.platform ==
'win32'):
while audioalert():
self.cnt += 1
else:
while audiodummy():
self.cnt += 1

def stop(self):
"""Set killaudio flag to 1, set stopthread Event"""
self.killaudio = 1
self.stopthread.set()
print self, " stop was called"

def audiodummy():
"""dummy audio playback"""
while t2.cnt >= 0 :
if t2.killaudio == 0:
print 'Beep!'
time.sleep(1)
else:
print 'audiodummy done'
return False
return True

def audioalert():
"""
Slightly expanded pygame/examples/sound.py
Playback sound and then test for audio channel usage
While channel is busy and while killaudio flag is set to 0
Check every second and return True at end of sound playback
When killaudio is set to 1 stop the playback and kill the mixer
Return False for ThreadTwo.run
NOTE: Copy tada.wav from X:\WINDOWS\Media\
"""
pgmixer.init(11025)
soundfile = os.path.join('.', 'tada.wav')
sound = pgmixer.Sound(soundfile)
channel = sound.play()
while channel.get_busy():
if t2.killaudio == 0:
pgtime.wait(1000)
else:
sound.stop()
pgmixer.quit()
return False
print '[DEBUG] loop number: ', t2.cnt
return True

t1 = ThreadOne()
t2 = ThreadTwo()

mainwindow = t1.dialog
mainwindow.show_all()
mainwindow.connect("destroy", lambda _: gtk.main_quit())

t1.start()
t2.start()

gtk.main()

print 't2.stop'
t2.stop()
print 't1.stop'
t1.stop()

##code end##

Thanks for your time.

Brendan Mchugh
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top