seg fault

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ajay

    #1

    seg fault

    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.z ip\\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.fram e, text="Enter message", justify=LEFT).g rid(row=0)
    self.entryMessa ge = Entry(self.fram e, width=25)
    self.entryMessa ge.grid(row=1, column=0)
    Button(self.fra me, text="Send", command=self.se ndMessage).grid (row=4,
    column=0)

    v_scrollbar = Scrollbar(self. frame, orient=VERTICAL )
    v_scrollbar.gri d(row=5, column=1, sticky=N+S)
    h_scrollbar = Scrollbar(self. frame, orient=HORIZONT AL)
    h_scrollbar.gri d(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=D ISABLED)

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

    self.phnumber = tkSimpleDialog. askstring("Phon e number", "Enter
    phonenumber", parent=self.fra me)
    if self.phnumber is None:
    sys.exit(0)

    self.th = threading.Threa d(target=self.s etElvin)
    self.th.start()

    def setElvin(self):
    elvin_con = elvin.connect(" elvin://praxis.it.usyd. edu.au")
    sub = elvin_con.subsc ribe("equals(ty pe, 'FREEVO_SMS_REP LY')")
    sub.add_listene r(self.answerMe ssage)
    sub.register()
    elvin_con.run()

    def sendMessage(sel f):
    message = self.entryMessa ge.get()
    if message == "":
    tkMessageBox.sh owerror("Error" , "Enter message", parent=self.fra me)
    return

    offset = "+1000"
    t=localtime()
    date=str(t.tm_m day) + "/" + str(t.tm_mon) + "/" + str(t.tm_year)
    time=str(t.tm_h our) + ":" + 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=of fset,
    SMSText=message )

    def answerMessage(s elf, sub, msg, insec, rock):
    print msg
    self.reply = msg.get("SMSMes sage")
    dest = msg.get("SMSDes tination")
    if dest == self.phnumber:
    self.frame.even t_generate('<<r eply>>', when='tail')

    def displayReply(se lf, event=None):
    self.answerText .config(state=N ORMAL)
    self.answerText .insert(INSERT, self.reply+"\n\ n")
    self.answerText .config(state=D ISABLED)


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








    ----------------------------------------------------------------
    This message was sent using IMP, the Internet Messaging Program.
  • Diez B. Roggisch

    #2
    Re: seg fault

    Ajay wrote:
    [color=blue]
    > 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?[/color]

    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.

    --
    Regards,

    Diez B. Roggisch

    Comment

    • Eric Brunel

      #3
      Re: seg fault

      Ajay wrote:[color=blue]
      > 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[/color]
      [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.toggleLabe l)
      th = threading.Threa d(target=self.r unThread)
      th.setDaemon(1)
      th.start()

      def run(self):
      self.root.mainl oop()

      def toggleLabel(sel f, 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_listen er(self.answerM essage)), make sure it correctly
      handles the global interpreter lock. If it doesn't, it may be the cause of your
      seg fault.

      HTH
      --
      - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
      PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

      Comment

      Working...