Starting a thread before wxPython bootup ... interesting.

Discussion in 'Python' started by Shafik, Oct 4, 2007.

  1. Shafik

    Shafik Guest

    Hello folks,

    I'm having an issue with mixing wxPython and threading ... I realize
    multi-threading always introduces subtle bugs, but the following
    scenario is just odd:

    I start a dummy thread, that does nothing but increment a counter and
    print its value to the screen, then afterwards, I start the wxPython
    application. I get nothing but weird behavior: sometimes the gui just
    crashes, sometimes I get an exception, sometimes it runs for a little
    but very slowly ...

    Anyone know whats going on? I have a dual-core T5500, so multi
    threading is piece of cake for it hardware -wise.


    Cheers,
    --Shafik
    Shafik, Oct 4, 2007
    #1
    1. Advertising

  2. Shafik

    Chris Mellon Guest

    On 10/4/07, Shafik <> wrote:
    > Hello folks,
    >
    > I'm having an issue with mixing wxPython and threading ... I realize
    > multi-threading always introduces subtle bugs, but the following
    > scenario is just odd:
    >
    > I start a dummy thread, that does nothing but increment a counter and
    > print its value to the screen, then afterwards, I start the wxPython
    > application. I get nothing but weird behavior: sometimes the gui just
    > crashes, sometimes I get an exception, sometimes it runs for a little
    > but very slowly ...
    >
    > Anyone know whats going on? I have a dual-core T5500, so multi
    > threading is piece of cake for it hardware -wise.
    >


    That's quite a broad and poorly specified problem to ask people to
    solve with no code. You get double minus points for mentioning an
    exception without saying what it was.

    Below is a script that has no demonstrable problems on my machine:

    import wx
    import threading

    if __name__ == '__main__':
    run = True
    def count():
    counter = 0
    while run:
    print counter
    counter += 1
    thread = threading.Thread(target=count)
    thread.start()
    app = wx.App(False)
    f = wx.Frame(None)
    f.Show()
    app.MainLoop()
    run = False



    Since the counter loop spins tightly, this causes some CPU contention
    and could quite possibly cause a non-trivial GUI to respond poorly,
    but that's to be expected.
    Chris Mellon, Oct 4, 2007
    #2
    1. Advertising

  3. Shafik

    [david] Guest

    Since the observed behaviour is clearly undefined,
    I forgive you for the poorly specified behaviour
    description: asking for a close description of random
    behaviour is just ridiculous.

    The most obvious point is that wx is not re-entrant
    or thread safe: you have to make it so by using
    wx.CallAfter()

    Since you are talking about a bug in your screen
    display while using a screen library, it would
    also be interesting it you described the environment:
    Which operating system? Which windowing system?
    Which wx library? Which Python? Which IDE? What code?

    [david]






    Shafik wrote:
    > Hello folks,
    >
    > I'm having an issue with mixing wxPython and threading ... I realize
    > multi-threading always introduces subtle bugs, but the following
    > scenario is just odd:
    >
    > I start a dummy thread, that does nothing but increment a counter and
    > print its value to the screen, then afterwards, I start the wxPython
    > application. I get nothing but weird behavior: sometimes the gui just
    > crashes, sometimes I get an exception, sometimes it runs for a little
    > but very slowly ...
    >
    > Anyone know whats going on? I have a dual-core T5500, so multi
    > threading is piece of cake for it hardware -wise.
    >
    >
    > Cheers,
    > --Shafik
    >
    [david], Oct 5, 2007
    #3
  4. Shafik

    Shafik Guest

    My apologies for not supplying more info, I am at work and I am
    technically not allowed to surf the web for anything.

    I am using Python 2.5.1, the latest wx version (2.8.xx, I dont recall
    exactly). This is running under windows XP under the latest version of
    cygwin (that could be the source of the issues too).

    I was just looking for a general way to approach wx and
    multithreading, not a detailed solution to my problem. There are lots
    of tutorials online that describe how to perform a time-consuming task
    in a separate thread in a wx-gui, but nothing that describes any other
    use cases.

    The real goal is to start the gui in a different thread, and have the
    main thread regain control. That seems like a feasible thing to be
    able to do, though I have been unsuccessful ... if anyone would like
    to suggest something without being covertly insulting, I'm all ears.


    Thanks,
    --Shafik

    On Oct 4, 5:58 pm, "[david]" <> wrote:
    > Since the observed behaviour is clearly undefined,
    > I forgive you for the poorly specified behaviour
    > description: asking for a close description of random
    > behaviour is just ridiculous.
    >
    > The most obvious point is that wx is not re-entrant
    > or thread safe: you have to make it so by using
    > wx.CallAfter()
    >
    > Since you are talking about a bug in your screen
    > display while using a screen library, it would
    > also be interesting it you described the environment:
    > Which operating system? Which windowing system?
    > Which wx library? Which Python? Which IDE? What code?
    >
    > [david]
    >
    > Shafik wrote:
    > > Hello folks,

    >
    > > I'm having an issue with mixing wxPython and threading ... I realize
    > > multi-threading always introduces subtle bugs, but the following
    > > scenario is just odd:

    >
    > > I start a dummy thread, that does nothing but increment a counter and
    > > print its value to the screen, then afterwards, I start the wxPython
    > > application. I get nothing but weird behavior: sometimes the gui just
    > > crashes, sometimes I get an exception, sometimes it runs for a little
    > > but very slowly ...

    >
    > > Anyone know whats going on? I have a dual-core T5500, so multi
    > > threading is piece of cake for it hardware -wise.

    >
    > > Cheers,
    > > --Shafik
    Shafik, Oct 5, 2007
    #4
  5. Shafik

    Chris Mellon Guest

    On 10/5/07, Shafik <> wrote:
    > My apologies for not supplying more info, I am at work and I am
    > technically not allowed to surf the web for anything.
    >
    > I am using Python 2.5.1, the latest wx version (2.8.xx, I dont recall
    > exactly). This is running under windows XP under the latest version of
    > cygwin (that could be the source of the issues too).
    >
    > I was just looking for a general way to approach wx and
    > multithreading, not a detailed solution to my problem. There are lots
    > of tutorials online that describe how to perform a time-consuming task
    > in a separate thread in a wx-gui, but nothing that describes any other
    > use cases.
    >


    The general way to approach wx and multithreading is to just do it. In
    the general case, the only caveat you need to be aware of is that wx
    is not threadsafe and all GUI calls need to called from the main
    thread.

    > The real goal is to start the gui in a different thread, and have the
    > main thread regain control. That seems like a feasible thing to be
    > able to do, though I have been unsuccessful ... if anyone would like
    > to suggest something without being covertly insulting, I'm all ears.
    >


    If you're not going to say what you did and how it didn't work, I
    don't know how you expect anyone to help you.
    Chris Mellon, Oct 5, 2007
    #5
  6. >
    > The real goal is to start the gui in a different thread, and have the
    > main thread regain control. That seems like a feasible thing to be
    > able to do, though I have been unsuccessful ... if anyone would like
    > to suggest something without being covertly insulting, I'm all ears.


    AFAIK some toolkits take it personally if they don't run in THE thread,
    meaning the main-thread. No idea if that applies to your situation, but
    as you continue to being vague, so are the answers.

    Diez
    Diez B. Roggisch, Oct 5, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Farrell
    Replies:
    0
    Views:
    315
    Doug Farrell
    Aug 14, 2003
  2. Richard Lamboj

    starting a thread in a nother thread

    Richard Lamboj, Jan 27, 2010, in forum: Python
    Replies:
    7
    Views:
    271
  3. Corobori

    Need some hints before starting to change a page

    Corobori, Mar 2, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    110
    Ken Cox [Microsoft MVP]
    Mar 4, 2005
  4. Replies:
    3
    Views:
    157
  5. Bootup

    , Jun 15, 2007, in forum: Javascript
    Replies:
    1
    Views:
    91
    Erwin Moller
    Jun 15, 2007
Loading...

Share This Page