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

Discussion in 'Python' started by Ville M. Vainio, Sep 6, 2008.

  1. 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!"
    Ville M. Vainio, Sep 6, 2008
    #1
    1. Advertising

  2. Ville M. Vainio

    Sean DiZazzo Guest

    Re: How to run PyOS_InputHook from python code (i.e. yield to eventloops)

    On Sep 6, 1:00 pm, (Ville M. Vainio) wrote:
    > 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)
    Sean DiZazzo, Sep 8, 2008
    #2
    1. Advertising

  3. Ville M. Vainio

    ville Guest

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

    Sean DiZazzo <> writes:


    >> 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)


    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()
    ville, Sep 13, 2008
    #3
  4. Re: How to run PyOS_InputHook from python code (i.e. yield to eventloops)

    ville wrote:

    > 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>
    Fredrik Lundh, Sep 13, 2008
    #4
    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. umdehoon
    Replies:
    0
    Views:
    328
    umdehoon
    Nov 18, 2004
  2. Replies:
    1
    Views:
    318
    Gabriel Genellina
    Apr 22, 2008
  3. Ville Vainio
    Replies:
    6
    Views:
    358
    Ville M. Vainio
    Jul 26, 2009
  4. Markus
    Replies:
    1
    Views:
    194
    Mark Hubbart
    Sep 27, 2004
  5. Michael Edgar
    Replies:
    13
    Views:
    268
    Brian Candler
    Apr 21, 2011
Loading...

Share This Page