question about threading

S

sinan .

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
 
D

Dennis Lee Bieber

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top