tkinter, threads and asyncore together

Discussion in 'Python' started by george.trojan@noaa.gov, Feb 11, 2004.

  1. Guest

    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_with_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
     
    , Feb 11, 2004
    #1
    1. Advertising

  2. Peter Hansen Guest

    wrote:
    >
    > 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_with_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.


    I don't know what, if anything, asyncore has for that, but it's
    "competition" 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
     
    Peter Hansen, Feb 11, 2004
    #2
    1. Advertising

  3. Jp Calderone Guest

    On Wed, Feb 11, 2004 at 07:11:58PM +0000, wrote:
    > 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_with_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.


    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
     
    Jp Calderone, Feb 11, 2004
    #3
  4. wrote:

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


    #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
     
    Josiah Carlson, Feb 11, 2004
    #4
  5. > #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.


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

    - Josiah
     
    Josiah Carlson, Feb 11, 2004
    #5
  6. Eric Brunel Guest

    Josiah Carlson wrote:
    > wrote:
    >
    >> 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_with_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
    >>

    >
    > #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.


    Yep: use the after method to call a function from the Tkinter main loop after a
    given delay. See
    http://www.pythonware.com/library/tkinter/introduction/x9507-alarm-handlers-and-other.htm

    HTH
    --
    - Eric Brunel <eric dot brunel at pragmadev dot com> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
     
    Eric Brunel, Feb 12, 2004
    #6
    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. Julia Goolia

    tkinter, sockets and threads together

    Julia Goolia, Sep 11, 2003, in forum: Python
    Replies:
    4
    Views:
    554
    Steve Holden
    Sep 12, 2003
  2. Joshua Moore-Oliva

    asyncore, asynchat and threads

    Joshua Moore-Oliva, Aug 4, 2004, in forum: Python
    Replies:
    4
    Views:
    1,093
    Joshua Moore-Oliva
    Aug 4, 2004
  3. Nathan Pinno
    Replies:
    1
    Views:
    392
  4. Nathan Pinno
    Replies:
    5
    Views:
    1,165
    Nathan Pinno
    Nov 19, 2005
  5. Pavel Kosina
    Replies:
    8
    Views:
    582
    Gabriel Genellina
    Dec 29, 2008
Loading...

Share This Page