tkinter, threads and asyncore together

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • george.trojan@noaa.gov

    tkinter, threads and asyncore together

    My application consists of Tkinter GUI that has to communicate with a
    remote
    server. The communication is bi-directional: the GUI responds to remote
    requests and user actions uch as pressing a button) should send messages

    to the server. I want to have the network interface implemented as a
    separate
    thread, as it may take a while to proccess incoming messages. My initial
    approach is the following

    ------ Queue --------------------
    | | <------ | asyncore | socket
    | GUI | ? |dispatcher_wit h_send| <------------------> server
    | | ------> | |
    ------ --------------------

    My question is how to communicate with asyncore (to send a message, or
    end the thread)? I started with creating an additional dispatcher class
    using unix domain socket, but is seems wrong, why use sockets within the
    same process? I might try file_dispatcher around a pipe, but maybe I
    should use different design altogether.

    George

  • Peter Hansen

    #2
    Re: tkinter, threads and asyncore together

    george.trojan@n oaa.gov wrote:[color=blue]
    >
    > My application consists of Tkinter GUI that has to communicate with a
    > remote
    > server. The communication is bi-directional: the GUI responds to remote
    > requests and user actions uch as pressing a button) should send messages
    >
    > to the server. I want to have the network interface implemented as a
    > separate
    > thread, as it may take a while to proccess incoming messages. My initial
    > approach is the following
    >
    > ------ Queue --------------------
    > | | <------ | asyncore | socket
    > | GUI | ? |dispatcher_wit h_send| <------------------> server
    > | | ------> | |
    > ------ --------------------
    >
    > My question is how to communicate with asyncore (to send a message, or
    > end the thread)? I started with creating an additional dispatcher class
    > using unix domain socket, but is seems wrong, why use sockets within the
    > same process? I might try file_dispatcher around a pipe, but maybe I
    > should use different design altogether.[/color]

    I don't know what, if anything, asyncore has for that, but it's
    "competitio n" Twisted has something called callFromThread( ). This
    method arranges for the specified callable to be called from within
    the asynchronous loop at the next opportunity, allowing a separate
    thread to communicate back to the objects in the async thread.

    Maybe asyncore has something similar? (Checking the docs for it,
    I can't see anything that would help.)

    -Peter

    Comment

    • Jp Calderone

      #3
      Re: tkinter, threads and asyncore together

      On Wed, Feb 11, 2004 at 07:11:58PM +0000, george.trojan@n oaa.gov wrote:[color=blue]
      > My application consists of Tkinter GUI that has to communicate with a
      > remote
      > server. The communication is bi-directional: the GUI responds to remote
      > requests and user actions uch as pressing a button) should send messages
      >
      > to the server. I want to have the network interface implemented as a
      > separate
      > thread, as it may take a while to proccess incoming messages. My initial
      > approach is the following
      >
      > ------ Queue --------------------
      > | | <------ | asyncore | socket
      > | GUI | ? |dispatcher_wit h_send| <------------------> server
      > | | ------> | |
      > ------ --------------------
      >
      > My question is how to communicate with asyncore (to send a message, or
      > end the thread)? I started with creating an additional dispatcher class
      > using unix domain socket, but is seems wrong, why use sockets within the
      > same process? I might try file_dispatcher around a pipe, but maybe I
      > should use different design altogether.[/color]

      You need something selectable to wake asyncore up from its call to
      select(). The easiest way to do this is with a pipe. You don't have to
      send any actual message over the pipe, though; one byte will be enough to
      get asyncore to call back into your code, then you can retrieve the message
      from wherever it is (eg, in a Queue.Queue).

      Jp


      Comment

      • Josiah Carlson

        #4
        Re: tkinter, threads and asyncore together

        george.trojan@n oaa.gov wrote:
        [color=blue]
        > My application consists of Tkinter GUI that has to communicate with a
        > remote
        > server. The communication is bi-directional: the GUI responds to remote
        > requests and user actions uch as pressing a button) should send messages
        >
        > to the server. I want to have the network interface implemented as a
        > separate
        > thread, as it may take a while to proccess incoming messages. My initial
        > approach is the following
        >
        > ------ Queue --------------------
        > | | <------ | asyncore | socket
        > | GUI | ? |dispatcher_wit h_send| <------------------> server
        > | | ------> | |
        > ------ --------------------
        >
        > My question is how to communicate with asyncore (to send a message, or
        > end the thread)? I started with creating an additional dispatcher class
        > using unix domain socket, but is seems wrong, why use sockets within the
        > same process? I might try file_dispatcher around a pipe, but maybe I
        > should use different design altogether.
        >
        > George
        >[/color]

        #to control asyncore
        while 1:
        asyncore.loop(. 1)
        if not fromgui.empty() :
        #handle messages from GUI

        Another option is to just have one thread, with the mainloop of Tkinter
        do a single asyncore.loop(0 ) call every .01 seconds or so. In wxPython
        I'd just use a wxTimer, I don't know if Tkinter has an equivalent.

        - Josiah

        Comment

        • Josiah Carlson

          #5
          Re: tkinter, threads and asyncore together

          > #to control asyncore[color=blue]
          > while 1:
          > asyncore.loop(. 1)
          > if not fromgui.empty() :
          > #handle messages from GUI
          >
          > Another option is to just have one thread, with the mainloop of Tkinter
          > do a single asyncore.loop(0 ) call every .01 seconds or so. In wxPython
          > I'd just use a wxTimer, I don't know if Tkinter has an equivalent.[/color]

          I meant asyncore.poll(. 1) and asyncore.poll(0 ) respectively.

          - Josiah

          Comment

          • Eric Brunel

            #6
            Re: tkinter, threads and asyncore together

            Josiah Carlson wrote:[color=blue]
            > george.trojan@n oaa.gov wrote:
            >[color=green]
            >> My application consists of Tkinter GUI that has to communicate with a
            >> remote
            >> server. The communication is bi-directional: the GUI responds to remote
            >> requests and user actions uch as pressing a button) should send messages
            >>
            >> to the server. I want to have the network interface implemented as a
            >> separate
            >> thread, as it may take a while to proccess incoming messages. My initial
            >> approach is the following
            >>
            >> ------ Queue --------------------
            >> | | <------ | asyncore | socket
            >> | GUI | ? |dispatcher_wit h_send| <------------------> server
            >> | | ------> | |
            >> ------ --------------------
            >>
            >> My question is how to communicate with asyncore (to send a message, or
            >> end the thread)? I started with creating an additional dispatcher class
            >> using unix domain socket, but is seems wrong, why use sockets within the
            >> same process? I might try file_dispatcher around a pipe, but maybe I
            >> should use different design altogether.
            >>
            >> George
            >>[/color]
            >
            > #to control asyncore
            > while 1:
            > asyncore.loop(. 1)
            > if not fromgui.empty() :
            > #handle messages from GUI
            >
            > Another option is to just have one thread, with the mainloop of Tkinter
            > do a single asyncore.loop(0 ) call every .01 seconds or so. In wxPython
            > I'd just use a wxTimer, I don't know if Tkinter has an equivalent.[/color]

            Yep: use the after method to call a function from the Tkinter main loop after a
            given delay. See


            HTH
            --
            - Eric Brunel <eric dot brunel at pragmadev dot com> -
            PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

            Comment

            Working...