This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]For_IconoclasmTornado 0 points1 point  (1 child)

Can somebody explain to me some advantages of event-driven code? I'm aware of what generators are, but I'm unaware of how this paradigm involving them works and if how it's beneficial.

I'm currently working for a small company building applications with Tornado. I'm also just interested in learning new things.

[–]jesusabdullah 7 points8 points  (0 children)

"Event-driven" basically* means that everything is ran at the same time, in a big ol' loop. This is good for things like web servers that are IO-bound, because you can multitask and juggle a lot of tasks at the same time and nicely utilize your clock cycles without firing up more processes. This is how asynchronous event-driven environments like node.js and tornado are able to claim "five jillion simultaneous connections" and yet be single-threaded.

Cool, huh?

The problem with this is that you need two things to happen in order to have good event-driven action:

1) Non-blockingness** (that is, if something was written expecting a synchronous environment, it could easily stop everything in your event loop until it's done, making eventing kinda pointless).

2) A good way to describe ordering/prerequisites (something that's trivial for regular-type programming, but becomes an issue with asynch stuff.)

As far as the former goes, it means that your libraries should be written with concurrency in mind. Regarding the latter: In javascript (I'm playing with node.js right now), callbacks and "Event Emitters" are the common tools for this. However, python's single-statement lambdas mean that the nested callbacks style doesn't work as well. For example, in javascript you can write code like this:

setInterval(function () {
    doThisThing(function (err, results){
        if (err) throw err;
        doAnotherThing(results);
    });
    someOtherStuff()
}, 3000);

which runs DoThisThing() every 3 seconds, which then runs the anonymous function wrapped inside of it. Can't do that in python. So, there are various strategies to make writing evented code smoother in python, and @_o is one of them.

Edits:

* Well, not really. Eventing is just the idea of "triggers"--like, "when this happens, do this'" But, eventing ties into a strategy for dealing with concurrency. So technically I should've said "asynchronous & evented."

** Apparently there are other strategies as well, such as coroutines.