Message3069
[Alan]
So, in summary, this whole area seems to be poorly understand, and
varyingly implemented.
Truer words were never spoken.
Excellent analysis Alan. From my view, the question is whether you can
you do a receive on an 'unbound' socket that has a timeout set. I also
understand that with the history of c and unix and the way Java has been
implemented, the design of a Jython solution is not easy.
I think the Python implementation is reasonable since if you don't set a
timeout and then do a recv on an 'unbound' port, the timeout defaults to
None which means the program will hang for ever and so it throws an
error. But, if you set a timeout, it allows you to do the recv and then
throws an exception when the timer kicks off, and then the program
continues.
Which means the following is really a 1 second delay;
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(1.0)
result = s.recvfrom(1024)
I understand your point that the recvfrom will never actually receive a
packet and so it should be an error. But consider the following:
i = 5
j = 10
print i
j is never used so is that an error? I don't know but in a language
like Python or Jython, I would expect it to run.
So:
----------------------------
import socket
i = 5
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
result = s.recvfrom(1024)
print i
-------------------
The print i statement will never be run, so I think it is reasonable
here to throw an exception on the recvfrom.
But, if you add the settimeout, then:
----------------------------
import socket
i = 5
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s = settimeout(1.0)
result = s.recvfrom(1024)
print i
----------------
This will/should delay for a second then print '5' and then terminate
normally.
I would expect that this is the way a program would work in Python or
Jython.
Now, when you do a sendto, then:
----------------------------
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto("testdata", ('192.168.1.200', 10000))
result = s.recvfrom(1024)
--------------------------------------
In this case,whether or not a timeout is set, if there is a program
receiving at 192.168.1.200 port 10000, it will receive the "testdata"
packet and the hostname and port of our program that is attached to s.
The script doesn't really care what the number of the port is that the
OS attached to s just so we get our data when packets arrive back at
that port.
Finally, consider a multi threaded case. (the actual threaded scripts
have not been tested)
----------------------------
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(2.0)
Have one thread that has access to s do the following:
while(True):
try:
result = s.recvfrom(1024)
except timeout, to:
pass
And another thread that shares s do this:
------------------------------------------
while (True):
time.sleep(10.0)
s.sendto("testdata", ("192.168.1.200", 10000))
------------------------------------------------
It seems reasonable to me to expect that to work.
I honestly don't know which way is 'right', but I think you are in for
a lot of push back if you make this change.
Let me know if you want more of my input. And have a good weekend. |
|
Date |
User |
Action |
Args |
2008-03-07 17:01:47 | rluse | set | spambayes_score: 0.000920137 -> 0.00092013733 recipients:
+ rluse, fwierzbicki, amak |
2008-03-07 17:01:47 | rluse | set | spambayes_score: 0.000920137 -> 0.000920137 messageid: <1204909307.68.0.991595816905.issue1005@psf.upfronthosting.co.za> |
2008-03-07 17:01:47 | rluse | link | issue1005 messages |
2008-03-07 17:01:47 | rluse | create | |
|