Message queueing

James J. Besemer jb at cascade-sys.com
Thu May 2 09:43:58 EDT 2002


This is just a shot in the dark, but have you looked at the "Queue"
module?  Chapter 7.6 in the libary reference.  It's NOT AT ALL a
window's message queue (if that's what you meant) but it can queue
arbitrary data and if you have a place to shove windows messages in a
multi-threading environment you could possibly include the queue in
front to buffer incoming data.

Below is sample code how I used it to queue incoming speech requests to
the sound card.

Hope it helps.

Regards

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax
mailto:jb at cascade-sys.com




import sys, os
import socket, thread
import Queue
import string
from select import *
# the next two are my lower-level library routines
from speak import *
from playsounds import *

# server port offered on localhost
PORT=50011
HOST=""

# two queues connect three threads

ReaderQ = Queue.Queue()
SpeakerQ = Queue.Queue()

# Server thread accepts socket connects and passes
# them on to the Reader.  The idea is to accept sockets
# as fast as possible (FWIW).  Given the small number of
# messages in this case, the separation between Server and Reader
# is of dubious value.

def Server():
        s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
        s.bind(( HOST, PORT ))

        speak( "Sound server is ready" )

        while 1:
                s.listen(3)
                conn, addr = s.accept()
                ReaderQ.put(( conn, addr ))

# Reader thread reads up all the data submitted by the client
# and forwards it to the Speaker thread.

def Reader():
        while 1:
                conn, addr = ReaderQ.get()
                text = ""
                while 1:
                        data = conn.recv( 1024 )
                        if not data:
                                break
                        text += data

                if text:
                        SpeakerQ.put(( conn, text ))
                else:
                        conn.close()

# Speaker thread actually causes the speech to be emitted.
# This is ultimately done by an external exe.  Speaker can take several
# seconds to perform each action, so the queue here is essential.
# We send a return code back to the client before closing
# the connection.

def Speaker():
        while 1:
                rc = -1
                conn, data = SpeakerQ.get()
                items = data.split( "\n" )

                for item in items:
                        if not item:
                                continue

                        # if it looks like a filename
                        # or has a "-f" prefix
                        # then play WAV file
                        # else speech to text

                        if item[0] in ['.', '/']:
                                rc = playsounds( item )

                        elif len( item ) > 2 and item[:2] == '-f':
                                rc = playsounds( item[2:].strip())

                        else:
                                rc = speak( item )

                        conn.send( str( rc ) + "\n" )

                conn.close()

# launch the threads
# (Server runs as primary thread)

def main():
        try:
                thread.start_new_thread( Reader, ())
                thread.start_new_thread( Speaker, ())

                Server()
        finally:
                speak( "Sound Server is exiting" )

main()








More information about the Python-list mailing list