DISQUS

DISQUS Hello! Homo-Adminus Blog is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Jump to original thread »
Author

Using epoll() For Asynchronous Network Programming

Started by Scoundrel · 9 months ago

General way to implement tcp servers is “one thread/process per connection”. But on high loads this approach can be not so efficient and we need to use another patterns of connection handling. In this article I will describe how to implement tcp-server with synchronous conn ... Continue reading »

11 comments

  • 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.
  • This is exactly what I was looking for, thanks for the great information.
  • Not if you use the aio functions.
  • Is it possible to use FD (file descriptor) meant for poll() with epoll()?
  • Eranga, yes.

    OT: Great tutorial, even though you never declared events.
    Well done Scoundrel.
  • 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?
  • Sonny,
    Just use google's translator. CLick this link to see a translation (should help a little):
    http://translate.google.com/translate?hl=en&...
  • 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
  • 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
  • занимательно, надо будет py-epoll погонять)
    тем более сейчас джангу перевёл на асинхронный сервер который использует epoll В)

    кстати если не ошибаюсь, результатом C10K problem стал сервер lighttpd
  • 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

Add New Comment

Returning? Login