VIdeo Converence Software: Tk and thread synchronization

Eric Brunel eric_brunel at despammed.com
Tue Oct 3 09:39:09 CEST 2006


On Mon, 02 Oct 2006 14:42:15 +0200, Paolo Pantaleo  
<paolopantaleo at gmail.com> wrote:
> Hi,
>
> I am going on writing my video conference software. I wrote the video
> grab, code/decode, and netwoark (multicast) transport.
>
> I have one thread doing this:
>
> [thread 1]
> while True:
>   for some times:
>     my_socket.recv() #blocking here
>     store data
>   compute image #here we have a complete new image to display
>
> Now, I was thinking to display the image in a  Tk window. But I think
> i will need a separate thread to run the mainloop() [thread 2], right?

I don't know if tk likes to be run in anything else than the main thread;  
never tested it. If you can use the main thread for tk, you'll probably  
have far less problems.

>  How can I signale to the Tk thread that a new image is ready to be
> shown? I was thinkin on using an event generated for the first
> (network) thread. But I don't know how to do it exactly. Any
> suggestion, please?

Quite simple: in the tk thread, do:

widget.bind('<<MyEvent>>', command)

Then in the secondary thread, do:

widget.event_generate('<<MyEvent>>', when='tail')

'<<MyEvent>>' can be anything, as long as you surround it with << & >>  
(these are custom events in tk). Don't forget to put the when='tail' in  
the event_generate call, or your binding may get called immediatly in the  
current thread.

> What if I access to Tk object form thread 1, is Tk thread safe?

General answer: no, it isn't. The only thing that seems to work everytime  
on tk widgets from secondary threads is generating events as above. If you  
try to do anything else, you may experience deadlocks or even crashes.

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list