views:

100

answers:

1

Does anyone know if I can call epoll.register from another thread safely?

Here is what I am imagining:

  • Thread 1: epoll.poll()
  • Thread 2: adding some fd to the same epoll object with epoll.register

http://docs.python.org/library/select.html

+1  A: 

I changed my answer after you changed the question.

This will not be "thread safe" in that each thread will impact the same epoll object. Registering a new fd to the epoll object will still do it to that object.

There's no reason for that particular object to have different states across separate threads, because in that scenario one should made one object for each thread.

So, short answer: Your setup will work.

In fact, the python stdlib http.server package uses that same method (just using poll instead of epoll). It creates a polling object in one thread, and uses a separate thread to poll it.

Tor Valamo
Thanks for the input. How sure are you that doing this won't corrupt epoll somehow? I couldn't find any documentation on this anywhere.
Unknown
Common sense dictates that making this thread safe gives hidden behaviours. And since it's not documented, then it's safe to assume that it isn't. This is also easily testable, and since poll isn't thread safe, then making epoll thread safe doesn't make sense either. There are a lot of "gut feelings", but they are pretty obvious gut feelings. ;)
Tor Valamo
Also, if it was made thread safe, then there would be no way to do this in a cross-thread way, and that would only limit functionality. But in the opposite sense, it's completely possible to make this thread safe, simply by making a separate object in each thread.
Tor Valamo