question about threading

Discussion in 'Python' started by sinan ., Aug 17, 2005.

  1. sinan .

    sinan . Guest

    hi i have a program that listens socket connection and serial device.
    in main program i am creating a socket then calling a class to listen
    socket under while 1: ,
    class soket(Thread):
    def __init__(self):
    Thread.__init__(self)
    self.host = '127.0.0.1'
    self.port = 4500
    self.data = ''
    try:
    self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    self.s.bind((self.host,self.port))
    self.s.listen(1)
    except:
    print 'error creating a socket !?!'
    def run(self):
    Thread.__init__(self)
    self.conn, self.addr = self.s.accept()
    self.data += self.conn.recv(1024)
    if not self.data:
    pass
    else:
    return trim(self.data)
    self.conn.close()


    in main after doing this ,


    try:
    s = AboutSocket.soket()
    s.setDaemon(1)
    s.start()
    except:
    print 'Error occured while using Threaded Socket !?!'

    after creating a socket i put bot soket and serial listeners in same while 1:

    while 1:
    skcgelen = s.run()
    print '\n\nreceived data from socket : ' + skcgelen.__str__()
    okunan = serial_readline(ser,'[ETX]')
    print '\n\nreceived data : ' + okunan.__str__()
    # ser.write('[ACK]')
    # database_relation(db,okunan)

    this works but when i send message to serial, the program waits for
    socket to receive message, if i send a message to a socket it prints
    both socket message and serial device message, how can i work them
    separetly. i want to see the message from the serial if received
    immediately and socket if socket receives immediately. i`ll put these
    incoming message to the list dynamically.
    note: serial device listener is not threaded.
    thanks
     
    sinan ., Aug 17, 2005
    #1
    1. Advertising

  2. On Wed, 17 Aug 2005 12:54:49 +0300, "sinan ."
    <> declaimed the following in comp.lang.python:

    > def run(self):
    > Thread.__init__(self)
    > self.conn, self.addr = self.s.accept()
    > self.data += self.conn.recv(1024)
    > if not self.data:
    > pass
    > else:
    > return trim(self.data)
    > self.conn.close()
    >

    Why are you creating a whole thread that just dies after the first
    data is received?
    >
    > in main after doing this ,
    >
    >
    > try:
    > s = AboutSocket.soket()
    > s.setDaemon(1)
    > s.start()
    > except:
    > print 'Error occured while using Threaded Socket !?!'
    >
    > after creating a socket i put bot soket and serial listeners in same while 1:
    >
    > while 1:
    > skcgelen = s.run()


    This is NOT how you are supposed to use threads. Threads are
    supposed run asynchronously. You should have a "while True:" loop INSIDE
    the run method (and you shouldn't be calling it directly, as I recall --
    the start() method will call the run()). run() should not use a "return"
    call.

    If you are on a UNIX/Linux system, you wouldn't use the thread, and
    instead use a select() call to find out if /either/ the socket or the
    serial port had data. select() only works with sockets on Windows, so
    that approach won't work.

    > print '\n\nreceived data from socket : ' + skcgelen.__str__()
    > okunan = serial_readline(ser,'[ETX]')


    This is also a synchronous action, to my knowledge -- a readline
    won't return until the end-of-line has been seen.

    > print '\n\nreceived data : ' + okunan.__str__()
    > # ser.write('[ACK]')
    > # database_relation(db,okunan)
    >
    > this works but when i send message to serial, the program waits for
    > socket to receive message, if i send a message to a socket it prints
    > both socket message and serial device message, how can i work them
    > separetly. i want to see the message from the serial if received
    > immediately and socket if socket receives immediately. i`ll put these
    > incoming message to the list dynamically.
    > note: serial device listener is not threaded.


    Neither of these is threaded as you are using them.


    Pseudo-code:

    mainQ = Queue.Queue()

    #SerialThread:
    #open serial port
    while True:
    data = serial_readline(ser, "[ETX]")
    mainQ.put(("SERIAL", data))

    #SocketThread:
    #create socket and listen
    while True:
    conn, addr = s.accept()
    data = conn.recv(1024)
    mainQ.put(("SOCKET", data))
    conn.close()


    #Main program
    create serial thread
    create socket thread
    while True:
    (who, data) = mainQ.get()
    if who == "SERIAL":
    process serial data
    elif who == "SOCKET":
    process socket data
    else:
    #protocol error -- something did a mainQ.put() with bad
    #arguments

    Note that, if you need to reply to the socket, you do NOT want the
    thread to close the connection -- and may need to pass conn as part of
    the mainQ.put() data so the main thread can access the connection (and
    close it).
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Aug 17, 2005
    #2
    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. Alina
    Replies:
    0
    Views:
    1,684
    Alina
    Jul 16, 2003
  2. John

    Threading question

    John, Sep 28, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    633
    =?Utf-8?B?RGF2ZSBCYWNoZXI=?=
    Oct 7, 2004
  3. Replies:
    9
    Views:
    1,055
    Mark Space
    Dec 29, 2007
  4. Steven Woody
    Replies:
    0
    Views:
    431
    Steven Woody
    Jan 9, 2009
  5. Steven Woody
    Replies:
    0
    Views:
    461
    Steven Woody
    Jan 9, 2009
Loading...

Share This Page