473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

question about threading

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
Aug 17 '05 #1
1 1514
On Wed, 17 Aug 2005 12:54:49 +0300, "sinan ."
<or*************@gmail.com> 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).
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Aug 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Harrison | last post by:
I am working on an application on Mac OS X that calls out to python via PyImport_ImportModule(). I find that if the imported module creates and starts a python thread, the thread seems to be...
5
by: Richard P | last post by:
I need some help on timers. My app is asp.net 1.1 website running in a shared hosting environment with a third-party service provider. I currently request and cache 20 - 40 remote RSS feeds. When a...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
13
by: William Stacey | last post by:
Using the following code sample: public byte Get() { // <= Possible to switch Here?? lock(syncLock) { //Do something in Get(). } }
6
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
4
by: Roger | last post by:
I have a function that is currently wrapped up in a Class so I can pass a variable to it. This function is going to be threaded out and I would like the class function to be able to update a...
6
by: DarkBlue | last post by:
My application makes several connections to a remote database server via tcp/ip. Usually all is fine,but occasionally the server is down or the internet does not work and then there is the 30 sec...
4
by: DBC User | last post by:
I have a background process which reads a table to see if there are any pending requests. If there are any, then it will start a worker thread (only 10 allowed at a time) and executes a method. In...
4
by: Steven | last post by:
I am taking an "advanced" VB.Net course via web at a state university toward an information science degree. This is my second VB class and I am kind of disappointed in it. This week we covered...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.