A beginner's question on thread

Discussion in 'Python' started by Timothy Wu, Apr 6, 2004.

  1. Timothy Wu

    Timothy Wu Guest

    I'm writing a small utility that listens for socket connections, and
    also repond to user inputs via a text menu selection.

    In order to respond to both the user and the incoming connections I
    figure I need to use thread.

    I think I would use the main thread to handle the menu and spawn a
    thread which listens on a socket.

    Now my question is, how do I terminate the server thread if user wants
    to exit the application? If the server just sit there and listen the
    thread would never terminate by itself. What kind of inter-thread
    communications are available and where would I find info/tutorial on that?

    Timothy
     
    Timothy Wu, Apr 6, 2004
    #1
    1. Advertising

  2. Timothy Wu

    Roger Binns Guest

    > Now my question is, how do I terminate the server thread if user wants
    > to exit the application?


    The easiest way is to setDaemon(True) on the thread. See the doc for
    exactly what it does.

    If you want to do it in a more "proper" fashion, then you have to
    make the thread do a polling loop, such as by putting a short
    timeout on the sockets, and then check a variable which indicates
    if you should continue, looping back if not.

    It would be really nice if Python supported interrupting a thread
    like Java does.

    Roger
     
    Roger Binns, Apr 6, 2004
    #2
    1. Advertising

  3. Timothy Wu wrote:
    > I'm writing a small utility that listens for socket connections, and
    > also repond to user inputs via a text menu selection.
    >
    > In order to respond to both the user and the incoming connections I
    > figure I need to use thread.
    >
    > I think I would use the main thread to handle the menu and spawn a
    > thread which listens on a socket.
    >
    > Now my question is, how do I terminate the server thread if user wants
    > to exit the application? If the server just sit there and listen the
    > thread would never terminate by itself. What kind of inter-thread
    > communications are available and where would I find info/tutorial on that?
    >
    > Timothy
    >


    Well, when the user wants to exit, the main thread could open a socket
    connection to the server thread, and send a special message telling it
    to stop.

    The server thread would have to distinguish between normal connections
    from clients and special connections from the main thread.

    Hope this helps

    Aurelio
     
    Aurelio Martin, Apr 6, 2004
    #3
  4. Timothy Wu

    Timothy Wu Guest

    Aurelio Martin wrote:

    > Well, when the user wants to exit, the main thread could open a socket
    > connection to the server thread, and send a special message telling it
    > to stop.
    >
    > The server thread would have to distinguish between normal connections
    > from clients and special connections from the main thread.
    >
    > Hope this helps
    >
    > Aurelio


    I've thought about doing that and I thought that was more of a hack.
    Is it the proper way to handle it?

    Timothy
     
    Timothy Wu, Apr 6, 2004
    #4
  5. Timothy Wu wrote:

    > I'm writing a small utility that listens for socket connections, and
    > also repond to user inputs via a text menu selection.
    >
    > In order to respond to both the user and the incoming connections I
    > figure I need to use thread.
    >
    > I think I would use the main thread to handle the menu and spawn a
    > thread which listens on a socket.
    >
    > Now my question is, how do I terminate the server thread if user wants
    > to exit the application? If the server just sit there and listen the
    > thread would never terminate by itself. What kind of inter-thread
    > communications are available and where would I find info/tutorial on that?



    No need for threads.

    If you have your server set up properly with asyncore, the below link
    will give you an example using wxPython that will just work...

    http://groups.google.com/groups?hl=...n&btnG=Search&meta=group%3Dcomp.lang.python.*

    Enjoy.

    - Josiah
     
    Josiah Carlson, Apr 7, 2004
    #5
  6. Timothy Wu

    Donn Cave Guest

    Quoth Timothy Wu <>:
    | I'm writing a small utility that listens for socket connections, and
    | also repond to user inputs via a text menu selection.
    |
    | In order to respond to both the user and the incoming connections I
    | figure I need to use thread.
    |
    | I think I would use the main thread to handle the menu and spawn a
    | thread which listens on a socket.
    |
    | Now my question is, how do I terminate the server thread if user wants
    | to exit the application? If the server just sit there and listen the
    | thread would never terminate by itself. What kind of inter-thread
    | communications are available and where would I find info/tutorial on that?

    Well, that's the deal. Use threads to solve a problem, and now
    you have two problems.

    I'd probably do it the way Aurelio Martin proposed, with I/O to
    the socket thread. It's much neater than whacking the thread,
    which is the alternative.

    I wouldn't necessarily use threads in the first place, though.
    I think it really depends on the user interface - usually that
    has to be the dispatch core of the application, and if it's
    built for threads, then there you go. Typically there will be
    some provision for dispatch on an I/O channel like a socket.
    If you're writing your own user interface, probably just reading
    and writing to the tty, and the socket thread is going to be
    the one and lonely extra thread - then don't do it, I'd say.
    Use select.select, or a package like asyncore or twisted or
    whatever (not very familiar with that territory, I just use select.)

    Donn Cave,
     
    Donn Cave, Apr 7, 2004
    #6
  7. Timothy Wu

    Timothy Wu Guest

    Donn Cave wrote:
    > Well, that's the deal. Use threads to solve a problem, and now
    > you have two problems.
    >
    > I'd probably do it the way Aurelio Martin proposed, with I/O to
    > the socket thread. It's much neater than whacking the thread,
    > which is the alternative.
    >
    > I wouldn't necessarily use threads in the first place, though.
    > I think it really depends on the user interface - usually that
    > has to be the dispatch core of the application, and if it's
    > built for threads, then there you go. Typically there will be
    > some provision for dispatch on an I/O channel like a socket.
    > If you're writing your own user interface, probably just reading
    > and writing to the tty, and the socket thread is going to be
    > the one and lonely extra thread - then don't do it, I'd say.
    > Use select.select, or a package like asyncore or twisted or
    > whatever (not very familiar with that territory, I just use select.)


    I think Roger Binns's setDaemon() suggestion works for me. =)
    Thanks for suggestion the other options. I'll take a look at asnycore or
    twisted when I have time or perhaps for other projects. Since from what
    I read somewhere else, it doesn't seem like Python thread's performance
    is exceptional.

    Timothy
     
    Timothy Wu, Apr 8, 2004
    #7
  8. Timothy Wu

    Timothy Wu Guest

    Donn Cave wrote:
    > Well, that's the deal. Use threads to solve a problem, and now
    > you have two problems.
    >
    > I'd probably do it the way Aurelio Martin proposed, with I/O to
    > the socket thread. It's much neater than whacking the thread,
    > which is the alternative.
    >
    > I wouldn't necessarily use threads in the first place, though.
    > I think it really depends on the user interface - usually that
    > has to be the dispatch core of the application, and if it's
    > built for threads, then there you go. Typically there will be
    > some provision for dispatch on an I/O channel like a socket.
    > If you're writing your own user interface, probably just reading
    > and writing to the tty, and the socket thread is going to be
    > the one and lonely extra thread - then don't do it, I'd say.
    > Use select.select, or a package like asyncore or twisted or
    > whatever (not very familiar with that territory, I just use select.)


    I think Roger Binns's setDaemon() suggestion works for me. =)
    Thanks for suggestion the other options. I'll take a look at asnycore or
    twisted when I have time or perhaps for other projects. Since from what
    I read somewhere else, it doesn't seem like Python thread's performance
    is exceptional.

    Timothy
     
    Timothy Wu, Apr 8, 2004
    #8
    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. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    653
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Poldie
    Replies:
    4
    Views:
    5,777
    Poldie
    Mar 31, 2006
  3. Rensjuh
    Replies:
    7
    Views:
    1,041
    Mabden
    Sep 2, 2004
  4. vincent wehren
    Replies:
    0
    Views:
    454
    vincent wehren
    Dec 11, 2003
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    261
    7stud --
    Apr 12, 2011
Loading...

Share This Page