threads and sockets

Discussion in 'Python' started by Ajay, Oct 13, 2004.

  1. Ajay

    Ajay Guest

    hi!

    how can i stop a server socket running in a thread other than the main
    thread? if the server socket was a local variable of the function started
    by the child thread, would calling join work?

    thanks






    ----------------------------------------------------------------
    This message was sent using IMP, the Internet Messaging Program.
    Ajay, Oct 13, 2004
    #1
    1. Advertising

  2. Ajay wrote:

    > how can i stop a server socket running in a thread other than the main
    > thread? if the server socket was a local variable of the function started
    > by the child thread, would calling join work?


    Short answer: you can't, and join won't help. Use select on sockets, with a
    timeout, or use twisted.


    --
    Regards,

    Diez B. Roggisch
    Diez B. Roggisch, Oct 13, 2004
    #2
    1. Advertising


  3. > > how can i stop a server socket running in a thread other than the main
    > > thread? if the server socket was a local variable of the function started
    > > by the child thread, would calling join work?

    >
    > Short answer: you can't, and join won't help. Use select on sockets, with a
    > timeout, or use twisted.


    More specifically:

    def serverthread(sock):
    while not quit:
    try:
    select(sock, sock, sock, 1)
    except:
    #handle errors, but don't use a bare except in real code
    #close sock if necessary

    - Josiah
    Josiah Carlson, Oct 13, 2004
    #3
  4. Ajay

    Elbert Lev Guest

    Ajay <> wrote in message news:<>...
    > hi!
    >
    > how can i stop a server socket running in a thread other than the main
    > thread? if the server socket was a local variable of the function started
    > by the child thread, would calling join work?
    >


    Here is how you do what this:

    import socket, threading

    class MyThread(threading.Thread):
    def __init__(self):
    threading.Thread.__init__(self)
    HOST = ''
    PORT = 50007
    self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #! blocking operatioms on this socket (accept) will timeout
    self.s.settimeout(1)
    self.s.bind((HOST, PORT))
    self.s.listen(5)
    self._stop = False
    def stop(self):
    self._stop = True
    def run(self):
    while 1:
    try:
    if self._stop:
    print "stop requested"
    break
    conn, addr = self.s.accept()
    print "connection from", addr
    except socket.timeout:
    print "timeout"
    except socket.error:
    print "socket.error"
    break
    self.s.close()

    def TimerFucn(t):
    print "TimerFucn"
    t.stop()
    # you can do t.close(). accept() will throw an exception
    # in thos case all the stuff with stop() is not needed

    listener = MyThread()
    t = threading.Timer(10, TimerFucn, (listener,))
    t.start()
    listener.start()

    while 1:
    s = raw_input()
    if s[0] == 'q':
    break
    Elbert Lev, Oct 14, 2004
    #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. Jupiter5F

    Threads and Sockets

    Jupiter5F, Nov 14, 2003, in forum: C++
    Replies:
    4
    Views:
    2,291
    Jupiter5F
    Nov 14, 2003
  2. Julia Goolia

    tkinter, sockets and threads together

    Julia Goolia, Sep 11, 2003, in forum: Python
    Replies:
    4
    Views:
    522
    Steve Holden
    Sep 12, 2003
  3. Gonçalo Rodrigues

    Help needed in problem with Threads and sockets.

    Gonçalo Rodrigues, Dec 29, 2003, in forum: Python
    Replies:
    0
    Views:
    265
    Gonçalo Rodrigues
    Dec 29, 2003
  4. Rod Stephenson
    Replies:
    0
    Views:
    434
    Rod Stephenson
    Jun 22, 2004
  5. geoffbache
    Replies:
    6
    Views:
    383
    fumanchu
    Jun 11, 2007
Loading...

Share This Page