I'm using a couple of Python libraries, and they both use a select() syscall. I was wondering if it was safe to have two select() syscalls from within the same thread (assuming there are no shared descriptors between them)?
Well, within a single thread you can't really have "two select() syscalls", because the first call has to end before you can start the second call. But yes, it's perfectly safe, even if they do share descriptors: both calls create new objects to return, there's no variable re-use that might affect them (like it might with static
variables in C.)
Even if you had multiple threads it would be fine, as the select
module is careful to hold the GIL until the actual select
call. In that case you do have to avoid sharing descriptors, though.
It seems unlikely to me that this is going to work well, but it depends on the libraries, and what you need them to do. Some thoughts:
- The libraries might provide APIs to open up the select loop, either by letting you plug into their select loop with your own file descriptors, or by providing APIs that return their own selectors (so that you can wire them into your own select loop), or both. I'd look into whether these libraries have such APIs.
Assuming no such APIs exist, then:
If either of the libraries implements the select loop such that it never returns from the loop, then you're totally out of luck -- you'll never be able to go one that library's select loop into the other library's select loop.
If either of the libraries call
select()
without a timeout value, you're probably not going to get good results. Without a timeout, the library can (and probably will) cause the thread to block indefinitely.If either library provides services that work best with low latency (for example, GUIs generally need to respond quickly to events), then having both libraries on the same thread is probably not a good idea.
The libraries may provide APIs that you can use to send messages that they'd pick up within the
select
loop. If they do, then this makes multi-threading that much easier to implement. If they don't, it may make even single-threading that much harder to manage. (Are you sure that multi-threading isn't an option?)Are you sure that your choice of libraries is appropriate for your app? If they're custom (or open source) libraries, can they be redesigned to make them more select-friendly?