9

I am writing now writing some evented code (In python using gevent) and I use the nginx as a web server and I feel both are great. I was told that there is a trade off with events but was unable to see it. Can someone please shed some light?

James

James
  • 605
  • 1
  • 5
  • 9

2 Answers2

9

The only difficulty of evented programming is that you mustn't block, ever. This can be hard to achieve if you use some libraries that were designed with threads in mind. If you don't control these libraries, a fork() + message ipc is the way to go.

Tobu
  • 24,771
  • 4
  • 91
  • 98
1

Biggest issue is that without threads, a block for one client will cause a block for all client. For example, if one client requests a resource (file on disk, paged-out memory, etc) that requires the OS to block the requesting process, then all clients will have to wait. A multithreaded server can block just the one client and continue to serve others.

That said, if the above scenario is unlikely (that is, all clients will request the same resources), then event-driven is the way to go.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
  • 7
    What you didn't mention is that you can just access the resource you need in a non-blocking way. That way you don't block, can keep answering clients, and will get a callback from your event system when the resource is ready, all in a single thread. – nosklo Nov 10 '10 at 03:55
  • 1
    second that -- the idea of event-based code is to use asynchronous interfaces for everything: no synchronous I/O. though with python even with gevent, twisted, etc it's fairly easy to accidentally use some blocking call if you're not careful. btw, this is the page everyone cites when talking about threads vs. events: http://www.kegel.com/c10k.html – cce Nov 24 '10 at 06:52
  • 2
    @cce: if c10k is not enough we could try c500k: http://blog.urbanairship.com/blog/2010/09/29/linux-kernel-tuning-for-c500k/ – jfs Nov 27 '10 at 13:53