How to run PyOS_InputHook from python code (i.e. yield to event loops)

  • Thread starter Ville M. Vainio
  • Start date
V

Ville M. Vainio

Background: PyOS_InputHook is something that gets run when python is
doing raw_input. TkInter and friends use it to run their event loops,
so that their events are handled while python is doing raw_input.

What I'd like to do is run the same function without having to do
raw_input. I.e. I'd like to run whatever event loop is available,
without incorporating any gui-specific code (PyOS_InputHook seems like
a nifty way to accomplish this).

My actual use case is to keep a tkinter application responsive while
launching a background process (and waiting for it to complete!).

My eventual code would be something like:

launch_process_in_thread('bzr pull')

while not is_done:
pyos_inputhook()
time.sleep(0.1)

print "Done!"
 
S

Sean DiZazzo

Background: PyOS_InputHook is something that gets run when python is
doing raw_input. TkInter and friends use it to run their event loops,
so that their events are handled while python is doing raw_input.

What I'd like to do is run the same function without having to do
raw_input. I.e. I'd like to run whatever event loop is available,
without incorporating any gui-specific code (PyOS_InputHook seems like
a nifty way to accomplish this).

My actual use case is to keep a tkinter application responsive while
launching a background process (and waiting for it to complete!).

My eventual code would be something like:

launch_process_in_thread('bzr pull')

while not is_done:
  pyos_inputhook()
  time.sleep(0.1)

print "Done!"

I'm still recovering from a hangover, so don't quote me. I think you
want the "after" function:

launch_process_in_thread('bzr pull')
self.update()

def update(self):
while not self.is_done:
self.after(2000, self.update)
 
V

ville

I'm still recovering from a hangover, so don't quote me. I think you
want the "after" function:

launch_process_in_thread('bzr pull')
self.update()

def update(self):
while not self.is_done:
self.after(2000, self.update)

That's tk-specific, right? I'm looking for a snippet that

- Would not be tied to tk

- Would run sequentially, i.e. the next command would not be entered
before the process has finished. Just like os.system()
 
F

Fredrik Lundh

ville said:
That's tk-specific, right? I'm looking for a snippet that

- Would not be tied to tk

upstream, you said:

"My actual use case is to keep a tkinter application responsive"

</F>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top