Questions to threads

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Questions to threads

franz steinhaeusler
Hello,

I was longer time away of python programming in general, so please excuse my non-knowledge.

I have two questions:

1) in C#, often one uses thread.sleep() in loops, (I don't know, if that is ok anyway), to give mainthread and thread time.
Is there any necessity to prevent cpu overusage in python?
the thread subject looks big and confusing (for example, there are two moduls, thread and threading).
Is there a difference in thread handling in python and wxPython?

Anyone knows a good link, which explains only the most importing issues about threading?

2) actual case:

I don't have a runnable code now, so I try to explain in pseudo code:

Threadstatt
[code]
here I post a command event, which should show a custom dialog
and her I want to wait in the thread, until the custom dialog is left by ok or cancel button.
[code]
Threadend

What thread events/locks (or how), ... should I use to wait for the dialog is finished?
(I have used a global variable to indicate the dialog is closed, but this can never be the proper way).

So basically, I would need some base knowledge about threads.

Thank you!

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Questions to threads

James Scholes
franz steinhaeusler wrote:
> What thread events/locks (or how), ... should I use to wait for the
> dialog is finished?

You don't need to use threading here at all, and if you do the results
will probably be quite unpredictable.  In wxPython you should always
avoid directly querying or controlling wxPython objects from anything
other than the main thread.  In other words, you should not create a
second, background thread and then directly monitor the state of the
dialog.  That is the wrong solution here.

Instead, just bind a handler to the dialog's wx.EVT_CLOSE event and the
handler will be called when the dialog is closed.  This is assuming that
the dialog isn't modal - if it is modal and you use the ShowModal
method, that call will block until the dialog closes so that's another
way to be notified.  Just wait for it to return.
--
James Scholes
http://twitter.com/JamesScholes

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Questions to threads

Tim Roberts
In reply to this post by franz steinhaeusler
franz steinhaeusler wrote:
>
> 1) in C#, often one uses thread.sleep() in loops, (I don't know, if
> that is ok anyway), to give mainthread and thread time.

That is NOT a common practice in quality code, even in C#.  The
operating systems are FAR better at managing CPU assignments than your
application will ever be.  Your applications should just use the time
they need, and let the system manage the balancing.


> Is there any necessity to prevent cpu overusage in python?

No, just like there is no need to do so in C#.  You have an incorrect
mental model.


> Is there a difference in thread handling in python and wxPython?

The issues are the same as what you face in C#.  All GUI manipulation
must be done in the main thread.  With C#, you use Invoke and
InvokeRequired to switch to the main thread for GUI stuff.  With
wxPython, you send messages, or use wx.CallAfter when you need to tweak
the UI.


> 2) actual case:
>
> Threadstatt
> [code]
> here I post a command event, which should show a custom dialog
> and her I want to wait in the thread, until the custom dialog is left
> by ok or cancel button.
> [code]
> Threadend
>
> What thread events/locks (or how), ... should I use to wait for the
> dialog is finished?

James gave you excellent advice here.  When you're working with an
event-driven systems (as all modern UI frameworks are), you have to
start thinking in terms of events.  If you have a thread that needs to
wait for a UI event, then you need to think about splitting the task
into two parts.

    Triggering event:
        Start thread part 1

    Thread 1
        Compute
        Compute
        Send event to main thread and exit

    Event handler
        Display dialog

    Dialog OK event:
        Start thread part 2

    Thread part 2
         Compute
         Compute

Notice how there is never anything just spinning in a wasteful loop.
The two parts of the thread could be one function with a parameter, or
you could have a state variable that tells you how far you have progressed.

--
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Questions to threads

jamesw
In reply to this post by franz steinhaeusler
Hi Franz,

Anyone knows a good link, which explains only the most importing issues about threading?


To the Phoenix gurus, I just noticed that the Phoenix docs say that wx.PostEvent "is not thread-safe for event objects having String fields, use wx.QueueEvent instead.": https://wxpython.org/Phoenix/docs/html/wx.functions.html#wx.PostEvent but it looks like this warning was automatically generated from the C++ wxWidgets source files, and might not apply to the Python implementation?

Cheers,
James

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Questions to threads

Robin Dunn
James Wettenhall wrote:
Hi Franz,

Anyone knows a good link, which explains only the most importing issues about threading?


To the Phoenix gurus, I just noticed that the Phoenix docs say that wx.PostEvent "is not thread-safe for event objects having String fields, use wx.QueueEvent instead.": https://wxpython.org/Phoenix/docs/html/wx.functions.html#wx.PostEvent but it looks like this warning was automatically generated from the C++ wxWidgets source files, and might not apply to the Python implementation?

IIRC, the problem that caused the need to add QueueEvent is pretty obscure and probably less likely to happen in Python applications as long as the event object's Clone method is implemented properly.  If you are posting custom classes derived from wx.PyEvent or wx.PyCommandEvent then those will always be safe.

OTOH QueueEvent is probably a better fit in either case since it transfers ownership instead of cloning the event object, which is how most other things in wx work. Otherwise it works the same as PostEvent. QueueEvent's main problem is that it is not well known yet. ;-)


--
Robin Dunn
Software Craftsman
http://wxPython.org

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Questions to threads

franz steinhaeusler
Hello all,

thank you for your useful answers.
James Wettenhall: also for your links; this is exactly, what I looked for.

Maybe someone is interested in the background of my last questions: I again brought up a very(!) old project, which I never finished around 13 ord 14 years ago! ;)

wypyatol (https://sourceforge.net/projects/wxpyatol/files/wxpyatol/)

a filemanager, which I "discovered again" and wanted to implement from the base of atol (http://atol.sourceforge.net/)

(I must admit, it is a huge mess, and first steps is, to make it runnable and clean up the code) and I'm not sure if I have time, motivation enough to bring it at least into usable state.
Years I didn't almost anything with wxPython. ;)

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...