DISQUS

Homo-Adminus Blog: Using epoll() For Asynchronous Network Programming

  • David Schwartz · 3 years ago
    It should be pointed out that if you use this approach, all code from handle_io_on_socket must avoid blocking no matter what. This can be nearly impossible in an application that's not multi-threaded.
  • connection · 3 years ago
    This is exactly what I was looking for, thanks for the great information.
  • Nathan Mickulicz · 2 years ago
    Not if you use the aio functions.
  • Eranga · 2 years ago
    Is it possible to use FD (file descriptor) meant for poll() with epoll()?
  • MunkeN · 2 years ago
    Eranga, yes.

    OT: Great tutorial, even though you never declared events.
    Well done Scoundrel.
  • Sonny · 2 years ago
    I found a website with an epoll example written by zhoulifa(zhoulifa@163.com). Its comments are in chinese, could anyone translate it/document it in english so it will be understood better? http://zhoulifa.bokee.com/6081520.html

    Btw, why must it avoid nonblocking on function handle_io_on_socket? It accessing db like MySQL nonblocking?
  • Mr Mister · 2 years ago
    Sonny,
    Just use google's translator. CLick this link to see a translation (should help a little):
    http://translate.google.com/translate?hl=en&...
  • Prashanth · 1 year ago
    Hi,

    Thanks for the info for epoll. These are helpful.

    I had one question regarding the user data variable given as part of epoll_event structure.
    If only "fd" is used for epolling, why are u32/u64 and void pointers provided.

    thanks,
    Prashanth
  • James · 1 year ago
    actually "fd" is not "used" for epolling. The fd is passed separately to epoll_wait, the data structure is used for passing in any data the user requires. As it is a union structure writing to "void *ptr" will overwrite fd.

    This data structure is useful for passing in data that may be useful to the user of the data. For example you can cache data that has been gathered by the connection previously and then store this in a structure which the void *ptr points to. When more data is ready you then have access to the previously stored data which you can add to with the further communication.

    James
  • slav0nic · 1 year ago
    занимательно, надо будет py-epoll погонять)
    тем более сейчас джангу перевёл на асинхронный сервер который использует epoll В)

    кстати если не ошибаюсь, результатом C10K problem стал сервер lighttpd
  • addisu · 1 year ago
    Great write up. I couldn't find a more detailed example than this. However, how do you know when a client closes a connection? It seems that you should be able to check your event against EPOLLHUP but the event number shown when a client closes is 0x5 while EPOLLHUP is defined as 0x10. So is there some bit masking I have to do?

    Thanks,
    Addisu