For whatever reason, a Python thread will not yield when waiting for a
packet on next(). For instance,
self.p = pcap.pcapObject()
self.p.open_live( self.interfaceName, 65535, True, 0 )
while True:
packetStringGlob = self.p.next()
print "I got one!"
If you open this on an interface which receives no traffic (not hard to do
if you open on all interfaces and one of them isn't configured) on many
threads, your program will hang on next(), even though you put it in a
thread and it will wait on a blocking call.
A workaround is as follows:
self.p = pcap.pcapObject()
# return after 1ms
self.p.open_live( self.interfaceName, 65535, True, 1 )
while True:
if packetStringGlob == None:
time.sleep( 1 )
packetStringGlob = self.p.next()
if packetStringGlob == None:
continue
print "I got one!"
The upshot is that if the buffer fills up, it won't immediately respond
until the sleep() is over. If there are packets, it'll return packets, and
therefore won't sleep before it emptys the buffer. It's a lot like the
behavior of the top snippet that doesn't work... only it does work in
threaded environments.
Hope this helps someone, and hopefully it'll get fixed at some point.
Nobody/Anonymous
None
None
Public
|
Date: 2009-03-02 04:11 Hi, |
Copyright © 2009 Geeknet, Inc. All rights reserved. Terms of Use