Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources about Asynchronous Programming Design Patterns

Tags:

asynchronous

I'm looking for non-trivial resources on concepts of asychronous programming, preferably books but also substantial articles or papers. This is not about the simple examples like passing a callback to an event listener in GUI programming, or having producer-consumer decoupled over a queue, or writing an onload handler for your HTML (although all those are valid). It's about the kind of problems the lighttpd developers might be concerned with, or someone doing substantial business logic in JavaScript that runs in a browser or on node.js. It's about situations where you need to pass a callback to a callback to a callback ... about complex asynchronous control-flows, and staying sane at the same time. I'm looking for concepts that allow you to do this systematically, to reason about this kind of control-flows, to seriously manage a significant amount of logic distributed in deeply nested callbacks, with all its ensuing issues of timing, synchronization, binding of values, passing of contexts, etc.

I wouldn't shrink away from some abstract explorations like continuation-passing-style, linear logic or temporal reasoning. Posts like this seem to go into the right direction, but discuss specific issues rather than a complete theory (E.g. the post mentions the "reactor" pattern, which seems relevant, without describing it).

Thanks.

EDIT:

To give more details about the aspects I'm interested in. I'm interested in a disciplined approach to asynchronous programming, a theory if you will, maybe just a set of specific patterns that I can pass to fellow programmers and say "This is the way we do asynchronous programming" in non-trivial scenarios. I need a theory to disentangle layers of callbacks that randomly fail to work, or produce spurious results. I want an approach which allows me to say "If we do it this way, we can be sure that ...". - Does this make things clearer?

EDIT 2:

As feedback indicates a dependency on the programming language: This will be JavaScript, but maybe it's enough to assume a language that allows higher-order functions.

EDIT 3:

Changed the title to be more specific (although I think design patterns are only one way to look at it; but at least it gives a better direction).

like image 938
ThomasH Avatar asked May 09 '11 12:05

ThomasH


People also ask

What are the three main patterns of asynchronous?

The three patterns discussed here are callbacks, promises, and async/await. There are other patterns as well as multiple variations of each so this post might expand in the future.

What is asynchronous design pattern?

In multithreaded computer programming, asynchronous method invocation (AMI), also known as asynchronous method calls or the asynchronous pattern is a design pattern in which the call site is not blocked while waiting for the called code to finish. Instead, the calling thread is notified when the reply arrives.

Which language is best for asynchronous programming?

Introduction. JavaScript is an asynchronous programming language in Node and in the browser. In many languages such as Java, C#, Python, etc.

What is asynchronous programming examples?

Asynchronous programming can help systems run more effectively, depending on the situation and often prevents long wait times. For example, if a task you want to perform uses a lot of input and output, asynchronous programming lets other tasks run, whereas synchronous programming would create a time block.


1 Answers

When doing layered callbacks currying is a useful technique.

For more on this you can look at http://en.wikibooks.org/wiki/Haskell/Higher-order_functions_and_Currying and for javascript you can look at http://www.svendtofte.com/code/curried_javascript/.

Basically, if you have multiple layers of callbacks, rather than having one massive parameter list, you can build it up incrementally, so that when you are in a loop calling your function, the various callback functions have already been defined, and passed.

This isn't meant as a complete answer to the question, but I was asked to put this part into an answer, so I did.

After a quick search here is a blog where he shows using currying with callbacks:

http://bjouhier.wordpress.com/2011/04/04/currying-the-callback-or-the-essence-of-futures/

UPDATE:

After reading the edit to the original question, to see design patterns for asynchronous programming, this may be a good diagram: http://www1.cse.wustl.edu/~schmidt/patterns-ace.html, but there is much more to good asynchronous design, as first-order functions will enable this to be simplified, but, if you are using the MPI library and Fortran then you will have different implementations.

How you approach the design is affected heavily by the language and the technologies involved, that any answer will fall short of being complete.

like image 118
James Black Avatar answered Sep 30 '22 16:09

James Black