wxYield and multiple threads

Hi all,

Being pretty new to wxPython (and python in general) it was recommended to insert a wxYield into some code that I had inherited to maintain:

class FooClass(threading.Thread)

[ the thread this manages the GUI]

self.foo = FooClass()

self.foo.daemon = True

–> proposed wx.Yield() here <—

self.foo.longRunningFunc()

self.foo.start()

The logic behind the suggestion would it would help the GUI be more responsive. But to me it would seem more elegant to rearrange the code so that we don’t call longRunningFunc() until after starting the background thread. Also it seemed strange to call functions in another thread directly- thinking it would be better to use some kind of messaging system (pubsub maybe) to alert the background thread to do some work.

Any thoughts?

Hi,

···

On Thu, Feb 21, 2013 at 2:19 PM, patrick korsnick korsnick@gmail.com wrote:

Hi all,

Being pretty new to wxPython (and python in general) it was recommended to insert a wxYield into some code that I had inherited to maintain:

class FooClass(threading.Thread)

[ the thread this manages the GUI]

self.foo = FooClass()

self.foo.daemon = True

→ proposed wx.Yield() here <—

self.foo.longRunningFunc()

self.foo.start()

The logic behind the suggestion would it would help the GUI be more responsive. But to me it would seem more elegant to rearrange the code so that we don’t call longRunningFunc() until after starting the background thread. Also it seemed strange to call functions in another thread directly- thinking it would be better to use some kind of messaging system (pubsub maybe) to alert the background thread to do some work.

Any thoughts?

Yea, your predecessor doesn’t to understand how threads work. The longRunningFunc should be called from within the thread objects (FooCass) ‘run’ method. When 'star’t is called it will invoke run on on a background thread.

Based on what is shown above it would appear that the thread is being created for no purpose.

Yield only causes a function in the main thread to yield control back to wx’s event loop so that it can process other pending window messages before returning control back to the code that called Yield.

Cody

Hi Cody,

Ok thanks for confirming what I suspected.

Cheers,

pat

···

On Thursday, February 21, 2013 1:19:44 PM UTC-7, patrick korsnick wrote:

Hi all,

Being pretty new to wxPython (and python in general) it was recommended to insert a wxYield into some code that I had inherited to maintain:

class FooClass(threading.Thread)

[ the thread this manages the GUI]

self.foo = FooClass()

self.foo.daemon = True

→ proposed wx.Yield() here <—

self.foo.longRunningFunc()

self.foo.start()

The logic behind the suggestion would it would help the GUI be more responsive. But to me it would seem more elegant to rearrange the code so that we don’t call longRunningFunc() until after starting the background thread. Also it seemed strange to call functions in another thread directly- thinking it would be better to use some kind of messaging system (pubsub maybe) to alert the background thread to do some work.

Any thoughts?