pylibpcap and multiple threads

Örjan Gustavsson orjang at gondolin.umunet.org
Tue Jan 25 10:58:08 EST 2005


Hi Carlos,

I looked in the source code for pylibpcap, and it does not release the 
GIL, hence my problem.

I found a solution though, the pcap object has a fileno member so I 
could use select to wait for a packet, then call loop without blocking. 
Problem solved! :)

And since it is only a thin wrapper over the pcap library it (seems to 
be) ok to open multiple instances of pcap, at least so long the 
different istances does select a separate interface. At least it works 
for me now!

And thank you for the link to http://libdnet.sourceforge.net it solved 
my next problem to send raw packets from python.

/Örjan Gustavsson

Carlos Ribeiro wrote:
> On Mon, 24 Jan 2005 15:18:39 +0100, Örjan Gustavsson
> <orjan at switchcore.com> wrote:
> 
>>Hi All!
>>
>>Sorry if this is not the correct forum for this kind of question (I did
>>not find any pylibpcap related lists).
>>
>>I am trying to use pylibpcap to capture network traffic from several
>>ethernet devices at the same time, each nic having a separate thread
>>assigned to it.
>>
>>My problem is that when one thread is blocking inside pcap.loop() for
>>instance, it seems to have acquired the GIL, so that no other threads
>>can run.
>>
>>Does anyone know a way to do this with threads, or is the only way to
>>have separate processes for each NIC?
> 
> 
>>From the top of my head, and without any test. I did use libpcap for
> some stuff recently. loop() is the primitive that keeps reading and
> calls the callback, right? I believe that it should release the GIL in
> the C wrapper code, right before calling the libpcap loop
> implementation, and re-acquire it before calling the callback. There
> is a standard way to do it, that is used before blocking calls are
> issued (file writing, for example). If it fails to release the GIL it
> will block.
> 
> I'm also not sure if the libpcap itself is supposed to be used this
> way - with multiple instances - but I assume that it should work (I
> never tried to open several instances of tcpdump at the same time).
> 
> Also, check these links also, as they may be helpful to you:
> 
> http://libdnet.sourceforge.net/
> http://oss.coresecurity.com/projects/pcapy.html
> 




More information about the Python-list mailing list