[Twisted-Python] libusb-1 file descriptors

Vincent Pelletier plr.vincent at gmail.com
Fri Mar 16 15:56:40 EDT 2012


First, sorry for non-threaded reply. I found the thread on gmane while 
googling a bit.

I'll try to explain the design and intended use of python-libusb1 polling 
integration. I lack twisted knowledge to tell how it could be integrated, 

As for the "poller" interface, it's python's select.poll interface.

Basically, libusb needs to be called into explicitly[1] whenever events 
needing some handling occur. It signals those events through a set of poll-
able file descriptors[2]. Alternatively (for OSes which cannot poll arbitrary 
file descriptors), LibUSBContext.getNextTimeout can be used to know for how 
long app can sleep (or do its own business) before next libusb event handling 
time... But that only works when libusb calls (transfer submission, 
synchronous or not) are all done in a single thread, otherwise they might last 
too long.

When several threads can simultaneously wait for events to occur (ex: a poller 
waiting on an asynchronous transfer with long timeout, then in a second thread 
a synchronous call expected to return quickly) a more complex scheme needs to 
be used. See libusb doc for the whole story.
In short, I decided to just write a stand-alone polling thread to support 
multi-thread access, as I couldn't figure a pythonic way of supporting several 
concurrent libusb-oriented pollers: C API relies on a mutable boolean-ish set 
by libusb code, and read by application code. As I try to never expose ctype 
instances to application code, and thought it should be enough to provide 
module user with a "start once, then forget" thread for handling this (much 
easier in python than in C), I went that way.
Besides the functions working with that boolean, I exposed all needed methods 
on LibUSBContext class, so different implementations can be done without 
hacking/working around, and hopefully without using libusb1 module directly 
(except for symbolic constants). I am opened to suggestions on how to expose 
the few missing functions in a pythonic way.

[1] LibUSBContext.handleEvents & co.
[2] LibUSBContext.getPollFDList, LibUSBContext.setPollFDNotifiers

Vincent Pelletier

More information about the Twisted-Python mailing list