python - Setting up idle thread/signalling thread -


i'm using python wxpython writing app.

the method i'm considering accomplish may not best - if that's case, let me know because i'm open refactoring.

right now, have 1 gui form. main program start point instantiates instance of gui form runs wx.mainloop(), causes app's main initial thread block lifetime of app.

we of course know when events happen in ui, ui thread runs code them.

now, have thread - worker thread. thread needs sit idle, , when happens in ui thread, e.g. button clicked, want worker thread stop idling , else - run function, say.

i can't envision right see app gets more complex having signal worker thread while it's busy doing something.

i have 2 questions setup:

  1. how can make worker thread idle without using cpu time? doing while true: pass suck cpu time, while while true: time.sleep(0.1) not allow instantaneous reaction events.

  2. what's best way signal worker thread something? don't want ui thread execute something, want worker thread signaled, ui thread, should change it's doing. ideally, i'd have way worker thread register callback ui itself, when button clicked or other ui event happens, worker thread signalled change it's doing.

so, best way accomplish this? , what's best way it?

thanks!

first: need background thread sit around idle in first place?

on platforms, starting new thread cheap. (except on windows , linux, it's supercheap.) so, why not kick off thread whenever need it? (it's easy keep around list of threads single thread, right?)

alternatively, why not create threadpoolexecutor, , submit jobs it, , let executor worry when run , on thread. time can think in terms of "tasks need run without blocking main thread" instead of "worker threads need wait on work", you're making life easier. under covers, there's still 1 or more worker threads waiting on queue, or equivalent, part's been written (and debugged , optimized) you. have write tasks, regular functions.

but, if want write explicit background threads, can, i'll explain that.


how can make worker thread idle without using cpu time? … what's best way signal worker thread something?

the way idle thread until value ready wait on synchronization object. on modern os, waiting on synchronization object means operating system stops giving cpu time until object ready you.*

there variety of different options can see in threading module docs, obvious 1 use in cases condition. way signal worker thread notify condition.

however, queue lot simpler. wait on queue, call get method block=true. signal thread wake up, put on queue. (under covers, queue wraps list or deque or other collection, lock, , condition, tell want do—check value, block until there's value, add value—instead of dealing waiting , signaling , protecting collection.)

see answer controlling ui elements in wxpython using threading how signal in both directions, worker thread ui thread , vice-versa.


i'd have way worker thread register callback ui itself, when button clicked or other ui event happens, worker thread signalled change it's doing.

you can way if want. pass self.queue.put or def callback(value): self.value = value; self.condition.notify() or whatever callback, , gui thread doesn't have know callback triggering thread.

in fact, that's pretty nice design may make happy later, when decide move code , forth between inline , background-threaded, or move off child process instead of background thread, or whatever.


i can't envision right see app gets more complex having signal worker thread while it's busy doing something.

but want happen if it's busy?

if want "if you're idle, wake , task; otherwise, hold onto , whenever you're ready", that's queue, or executor, automatically.

if want say, "if you're idle, wake up, otherwise, don't worry it", that's condition or event do.

if want say, "if you're idle, wake , this, otherwise, cancel you're doing , instead", that's bit more complicated. pretty need have background thread periodically check "interrupt_me" variable while it's busy (and put lock around it), , you'll set flag notifying condition… although in cases, can merge idle , busy cases single condition or event (by calling infinite wait() when idle, , quick-check wait(timeout=0) when busy).


* in cases—e.g., linux futex or windows criticalsection—it may spin off little bit of cpu time in cases, because happens optimization. point is, you're not asking cpu time until you're ready use it.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -