![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2005
Posts: 7
Rep Power: 0
![]() |
help with threads in python
Hi, I'm creating a simple client and server using threads in the client. My thread receives data from the server while the rest of my program keeps sending data as much as it wants. The problem is when I'm done sending data, the thread hangs. This is my first program with a thread. I tried putting a condition in so that when I finish sending, it would change the condition to false, but my thread did not respond to that. Any suggestion?
class getAck(threading.Thread):
def run(self):
while (1):
s.recv(line)
print line
s = socket(AF_INET, SOCK_DGRAM)
b = getAck()
b.start()
while( line != None ): #line.next returns None if no more data
s.sendto(line, addr)
line = line.next() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
By default, sockets in python are blocking. This means that if there is no data waiting to be received on the socket, they will keep waiting until there is some.
If you were using a TCP stream, then I believe when the other side closed down their connection, your socket would be closed also. (It's late, so I won't bother looking up the details). TCP is akin to a telephone call; it's clear when the other party hangs up. You, however, are using a stateless lossy UDP packet stream (with SOCK_DGRAM), which akin to sending postcards. Unlike a telephone call, I can't tell when the person I'm communicating with will stop sending postcards, unless I work out some sort of agreed system beforehand. For instance, the system for ending the communication's channel could be: If other person sends "QUIT", then reply with "ACK QUIT" (standing for; I've received your quit signal and am quitting), then quit. OR If no data is received for 60 seconds, then quit. Timeouts on sockets are pretty easy to set up: s = socket(AF_INET, SOCK_DGRAM) s.settimeout(60.0) # 60 second timeout |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2005
Posts: 7
Rep Power: 0
![]() |
I'm suppose to write a program that uses UDP and takes care of reliable data transfer for one of my cs lab.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
In which case, you'll need to have a system which acknowledges packets that have been sent, and a system which can retain the order the packets have been sent by. UDP doesn't guarentee that packets will get to their destination, nor in the right order.
A communications channel could look something like this (S = send, R = received): S: CONNECT 1 R: ACK 1 S: MESSAGE 2 Hello there S: MESSAGE 3 How are you? R: ACK 3 R: ACK 2 R: MESSAGE 1 I hear you loud S: ACK 1 R: MESSAGE 2 And clear S: ACK 2 S: QUIT 3 (( no ACK in 6 seconds; packet must have been lost )) S: QUIT 3 R: ACK 3 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2005
Posts: 7
Rep Power: 0
![]() |
Yeah, I understood that. It turned out I didn't understand what a thread was. I thought my thread was hanging, but it was just because my recv function was blocking. I threw in a condition to fix that though. If you're curious to the program I'm writing, here's the link: http://www.cs.ucr.edu/cs164/assignments/project.pdf
I'm 90% done with it. Just need the last part. Thanks for the replies. Your first reply made me realize the problem =D -Huy |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|