473,396 Members | 1,982 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.

More on Tk event_generate and threads

There have been a number of posts about calling gui methods from other
threads. Eric Brunel. has reccommended calling the gui's
..event_generate method with data passed thru a queue. This worked
great for me until trying to write to the gui from multiple threads.
There I had problems: random typesof crashes almost all resulting in
seg faults. I thought this info might help anyone trying to do the
sameor at least save some time debugging.

I wrote a (hopefully) simple and minimal piece of code to demonstrate
the problem included below. My knowledge of the Tcl/Tk interface is
minimal.

Another interesting thing is it fails on some systems and not on
others. All Linux:

Runs fine on:
Dual Opteron Fedora5
Linux xxxx 2.6.16-1.2096_FC5 #1 SMP Wed Apr 19 05:14:26 EDT 2006
x86_64 x86_64 x86_64 GNU/Linux

Singe Pentium Fedora4
uname info unavailable right now

Fails on:
Singe Xeon SuSE 10.1
Linux xxxx 2.6.16.13-4-smp #1 SMP Wed May 3 04:53:23 UTC 2006 i686
i686 i386 GNU/Linux

Single Celeron 400 (Embedded BlueCat)
Linux 2.6.7 #45 i686 i686 i386 (not copy pasted)

Single Celeron 400
SuSE 10.1 - not available
My solution (others have done the same) was to go back to having the
gui thread call its own event_generate method with the event string
passed in through queue and using a polling loop with after methods.

Would someone please verify that this shouldn't be done for some reason
such as thread-safety or point out what I'm doing wrong? It seems from
some of the errors that the event data is getting overwritten. I've
included the code, if anyone wants to see some of the errors I have
saved them.

---------------------------
from Tkinter import *
import threading
import Queue
from time import sleep
import random

class Thread_0(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
count=0
while True:
count+=1
hmi.thread_0_update(count)
sleep(random.random()/100)

class Thread_1(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
count=0
while True:
count+=1
hmi.thread_1_update(count)
sleep(random.random()/100)

class HMI:
def __init__(self):
self.master=Tk()
self.master.geometry('200x200+1+1')

f=Frame(self.master)
f.pack()

self.l0=Label(f)
self.l0.pack()
self.l1=Label(f)
self.l1.pack()

self.q0=Queue.Queue()
self.q1=Queue.Queue()

self.master.bind("<<Thread_0_Label_Update>>",self. thread_0_update_e)
self.master.bind("<<Thread_1_Label_Update>>",self. thread_1_update_e)

def start(self):
self.master.mainloop()
self.master.destroy()

#################################
def thread_0_update(self,val):
self.q0.put(val)
self.master.event_generate('<<Thread_0_Label_Updat e>>',when='tail')

def thread_1_update(self,val):
self.q1.put(val)
self.master.event_generate('<<Thread_1_Label_Updat e>>',when='tail')

def thread_0_update_e(self,e):
while self.q0.qsize():
try:
val=self.q0.get()
self.l0.config(text=str(val))
except Queue.Empty:
pass

def thread_1_update_e(self,e):
while self.q1.qsize():
try:
val=self.q1.get()
self.l1.config(text=str(val))
except Queue.Empty:
pass

##########################
if __name__=='__main__':
hmi=HMI()
t0=Thread_0()
t1=Thread_1()
t0.start()
t1.start()
hmi.start()

---------------------------

Jul 11 '06 #1
0 2190

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

Similar topics

0
by: Russell E. Owen | last post by:
In general, I thought one should not mess with Tkinter from a background thread. But Eric Brunel recently posted a clever suggestion that one could communicate from a background thread to the main...
0
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux...
0
by: indika | last post by:
Hi @ll! OS RedHat 9.0 DB2 8.1 unixODBC 2.2.9 I have a problem with the connection over unixODBC i cant open more than five threads then it will crash. CLI0108E Communication link failure....
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
10
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that...
4
by: | last post by:
I am working with an application that requires multithreading. I have found a sample online that I am currently working with/learning from. However, I am not sure if this is the most effecient...
10
by: Terry Olsen | last post by:
I need to be able to write to a file simultaneously from approximately 4 different threads. I'm working on a program that will download parts of a file and combine the parts. Each thread will have...
12
by: Peter K | last post by:
Say I have this class public class Simple { private string name; public string Name { get { return name; } set { name = value; }
82
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.