seg fault

A

Ajay

hi!

i have some code with a gui that does some processing in another thread,
say t2.
when t2 needs to display something on the gui, it generates an event (which
i have binded earlier). the code runs fine on windows but segfaults on
linux.
the point where it segfaults is when it generates the event.

any ideas why?

the code is

#app.py

import sys
sys.path.append('\\Program Files\\Python\\Lib\\python23.zip\\lib-tk')
sys.path.append('/n/filer1/u1/hons2004/rwhitake/elvin/local/usr/lib/python2.3/site-packages/')
import os
from Tkinter import *
import tkMessageBox
import tkFileDialog
import tkSimpleDialog
from Queue import Queue
from time import localtime
import threading

import elvin

class Phone:
def __init__(self, master):
self.frame = Frame(master)
self.frame.pack()

Label(self.frame, text="Enter message", justify=LEFT).grid(row=0)
self.entryMessage = Entry(self.frame, width=25)
self.entryMessage.grid(row=1, column=0)
Button(self.frame, text="Send", command=self.sendMessage).grid(row=4,
column=0)

v_scrollbar = Scrollbar(self.frame, orient=VERTICAL)
v_scrollbar.grid(row=5, column=1, sticky=N+S)
h_scrollbar = Scrollbar(self.frame, orient=HORIZONTAL)
h_scrollbar.grid(row=6, column=0, sticky=E+W)
self.answerText = Text(self.frame, width=25, height=15,
xscrollcommand=h_scrollbar.set, yscrollcommand=v_scrollbar.set)
self.answerText.grid(row=5, column=0, sticky=N+S+E+W)
self.answerText.config(state=DISABLED)

self.q = Queue()
self.reply=""
self.frame.bind("<<reply>>", self.displayReply)

self.phnumber = tkSimpleDialog.askstring("Phone number", "Enter
phonenumber", parent=self.frame)
if self.phnumber is None:
sys.exit(0)

self.th = threading.Thread(target=self.setElvin)
self.th.start()

def setElvin(self):
elvin_con = elvin.connect("elvin://praxis.it.usyd.edu.au")
sub = elvin_con.subscribe("equals(type, 'FREEVO_SMS_REPLY')")
sub.add_listener(self.answerMessage)
sub.register()
elvin_con.run()

def sendMessage(self):
message = self.entryMessage.get()
if message == "":
tkMessageBox.showerror("Error", "Enter message", parent=self.frame)
return

offset = "+1000"
t=localtime()
date=str(t.tm_mday) + "/" + str(t.tm_mon) + "/" + str(t.tm_year)
time=str(t.tm_hour) + ":" + str(t.tm_min) + ":" + str(t.tm_sec)
con = elvin.connect("elvin://praxis.it.usyd.edu.au")
con.notify(type="FREEVO_SMS", SMSSource=self.phnumber,
SMSDestination="foo", SMSDate=date, SMSTime=time, SMSGMTOffset=offset,
SMSText=message)

def answerMessage(self, sub, msg, insec, rock):
print msg
self.reply = msg.get("SMSMessage")
dest = msg.get("SMSDestination")
if dest == self.phnumber:
self.frame.event_generate('<<reply>>', when='tail')

def displayReply(self, event=None):
self.answerText.config(state=NORMAL)
self.answerText.insert(INSERT, self.reply+"\n\n")
self.answerText.config(state=DISABLED)


root = Tk()
phone = Phone(root)
root.mainloop()
 
D

Diez B. Roggisch

Ajay said:
hi!

i have some code with a gui that does some processing in another thread,
say t2.
when t2 needs to display something on the gui, it generates an event
(which i have binded earlier). the code runs fine on windows but segfaults
on linux.
the point where it segfaults is when it generates the event.

any ideas why?

Lots of gui-toolkits don't like to be manipulated outside the thread their
main eventloop is running. I don't know this for sure for tk, but your
problem looks like it's the case here.

So I suggest you communicate your event to the main thread using a queue,
and insert the event in tk's event loop somehow. For that you could use
after-calls that periodically check for pending events.
 
E

Eric Brunel

Ajay said:
hi!

i have some code with a gui that does some processing in another thread,
say t2.
when t2 needs to display something on the gui, it generates an event (which
i have binded earlier). the code runs fine on windows but segfaults on
linux.
the point where it segfaults is when it generates the event.

any ideas why?

the code is
[snip code]

Can you reproduce the problem with a simpler script? For example, does the
following script work for you:

---------------------------------------------------------------------
import time, threading
from Tkinter import *

class MyApp:

def __init__(self):
self.root = Tk()
self.ping = 0
self.lbl = Label(self.root, text='idle', width=6)
self.lbl.pack()
self.root.bind('<<go>>', self.toggleLabel)
th = threading.Thread(target=self.runThread)
th.setDaemon(1)
th.start()

def run(self):
self.root.mainloop()

def toggleLabel(self, ping):
if self.ping:
self.lbl['text'] = 'ping'
else:
self.lbl['text'] = 'pong'
self.ping = not self.ping

def runThread(self):
while 1:
time.sleep(1)
self.root.event_generate('<<go>>', when='tail')

app = MyApp()
app.run()
---------------------------------------------------------------------

I used this trick many times to make secondary threads communicate with a
Tkinter GUI, and never encountered a seg fault.

Since you apparently use custom modules, I'd say that the problem is more likely
to come from these modules, and not from Python or Tkinter. Is the elvin module
written in Python or in C or C++? If it is written in C or C++, since it uses a
Python callback (sub.add_listener(self.answerMessage)), make sure it correctly
handles the global interpreter lock. If it doesn't, it may be the cause of your
seg fault.

HTH
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top