I've encountered a thread safety problem with pcap after importing the pythoncom module in a separate thread. The code below exemplifies this behavior. Here, the Debug Thread is locked in the pcap.pcap method, so you never see the "PCap call successful" printout. On the other hand, if the pythoncom module is imported in the Debug Thread (uncomment the import statement at line 14 and comment out line 2), the code executes with no problem. Anyone have any ideas?
Im currently running:
python 2.5.2
pcap-1.1.win32-py2.5
pywin32-210.win32-py2.5
WinPcap 4.0.2
-------------------------------------
import pcap
import pythoncom
import threading
import imp
def main():
t = threading.Thread(name="Debug Thread", target=_launchpcap)
t.start()
t.join()
print "Debug Thread complete!"
def _launchpcap():
print "Debug Thread starting..."
ins = pcap.pcap(name="eth1", snaplen=1600, promisc=1, timeout_ms=500)
print "PCap call successful!"
if __name__ == "__main__":
main()
I don't know anything about pythoncom (or w32 generally), but I just fixed a bug (2080784) in which pylibpcap wouldn't release the GIL when it should. So the next release should work much better in multithreaded situations. Hopefully this will make it work better with pythoncom as well.
Version 0.6.3/0.6.4 releases the GIL when waiting for packets (and anything else that might block). Let me know if this doesn't fix the problem.