Making speech-dispatcher python API support asynchronous socket communication

Hemant Goyal goyal.hemant at gmail.com
Fri Jun 20 06:08:15 EDT 2008


Hi,

(This mail might become extremely verbose. Please bear with me)

The speech-dispatcher python API can be accessed at:
http://cvs.freebsoft.org/repository/speechd/src/python/speechd/client.py?view=markup

The client API communicates with the speech server presently in a thread by
polling the socket in the _communication() method. Whenever data is read
from the socket it is added to a buffer self._com_buffer.append((code, msg,
data)) and a semaphore value (self._ssip_reply_semaphore) is incremented. To
read data off this buffer _recv_response(self) is called and this method is
synchronized with the same semaphore self._ssip_reply_semaphore.

When a client instantiates a connection with the speech server a certain
amount of handshaking data is shared between the client API implementation
and speech server.

We are trying to eliminate the need for a thread by monitoring the socket
using gobject.io_add_watch(self._socket, gobject.IO_IN,
self._socket_read_cb). When the gtk mainloop is running and an input
activity is observed on the socket self._socket_read_cb will be called.
self._socket_read_cb in essence replicates _communication()

When the handshaking is taking place the gtk mainloop has not yet started
and hence we cannot rely on the event generated by gobject to read data off
the socket. And if this handshaking does not take place the API
implementation blocks the entire process thus leading to a deadlock. Now I
figured that we can allow the polling thread to exist until the handshaking
takes place and then shift to the gobject method of monitoring the socket. I
achieve this by defining a variable self._first_run. When the handshaking
completes I make the polling thread exit (by calling close_thread(self) from
the main control thread) . In this time frame I expect that the gtk mainloop
will start itself (I am quite sure that it does start actually).

Now the main problem:
the gobject method does not seem to run the callback to read data from the
socket when data is written to the socket. (I tried to test the socket
monitoring method in a separate script and it worked fine then). If the API
implementation is not able to read data from the buffer it blocks or
deadlocks...

I am unable to determine the exact mistake that I am making. Is there
something that I am missing in the way the API is implemented or is it a
problem with the way i am using gobject.io_add_watch?


Any clues/hints ?

Thanks,
Hemant
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.laptop.org/pipermail/devel/attachments/20080620/7a991c04/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: client.diff
Type: text/x-patch
Size: 5601 bytes
Desc: not available
URL: <http://lists.laptop.org/pipermail/devel/attachments/20080620/7a991c04/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: speech_mainloop_later.py
Type: text/x-python
Size: 3324 bytes
Desc: not available
URL: <http://lists.laptop.org/pipermail/devel/attachments/20080620/7a991c04/attachment.py>


More information about the Devel mailing list