1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
<!DOCTYPE html>
<html>
<head>
<title>
The Codex »
Stop Building Synchronous Web Containers
</title>
<link
rel='stylesheet'
type='text/css'
href='http://fonts.googleapis.com/css?family=Buenard:400,700&subset=latin,latin-ext'>
<link
rel="stylesheet"
type="text/css"
href="../media/css/reset.css">
<link
rel="stylesheet"
type="text/css"
href="../media/css/grimoire.css">
</head>
<body>
<div id="shell">
<ol id="breadcrumbs">
<li class="crumb-0 not-last">
<a href="../">index</a>
</li>
<li class="crumb-1 not-last">
<a href="./">dev</a>
</li>
<li class="crumb-2 last">
stop-building-synchronous-web-containers
</li>
</ol>
<div id="article">
<h1 id="stop-building-synchronous-web-containers">Stop Building Synchronous Web Containers</h1>
<p>Seriously, stop it. It's surreally difficult to build a sane ansynchronous service on top of a synchronous API, but building a synchronous service on top of an asynchronous API is easy.</p>
<ul>
<li>
<p>WSGI: container calls the application as a function, and uses the return
value for the response body. Asynchronous apps generally use a non-WSGI
base (see for example <a href="http://bottlepy.org/docs/dev/async.html">Bottle</a>).</p>
</li>
<li>
<p>Rack: container calls the application as a method, and uses the return
value for the complete response. Asynchronous apps generally use a non-Rack
base (see <a href="https://github.com/rkh/async-rack/issues/5">this Github ticket</a>).</p>
</li>
<li>
<p>Java Servlets: container calls the application as a method, passing a
callback-bearing object as a parameter. The container commits and closes
the response as soon as the application method returns. Asynchronous apps
can use a standard API that operates by <em>re-invoking</em> the servlet method as
needed.</p>
</li>
<li>
<p>What does .Net do?</p>
</li>
</ul>
<p>vs</p>
<ul>
<li>ExpressJS: container calls the application as a function, passing a
callback-bearing object as a parameter. The application is responsible for
indicating that the response is complete.</li>
</ul>
<h2 id="synchronous-web-containers-are-bad-api-design">Synchronous web containers are bad API design</h2>
<ul>
<li>
<p>Make the easy parts easy (this works)</p>
</li>
<li>
<p>Make the hard parts possible (OH SHIT)</p>
</li>
</ul>
<h2 id="writing-synchronous-adapters-for-async-apis-is-easy">Writing synchronous adapters for async APIs is easy</h2>
<pre><code>def adapter(request, response_callback):
synchronous_response = synchronous_entry_point(request)
return response_callback(synchronous_response)
</code></pre>
<p>Going the other way is more or less impossible, which is why websocket
support, HTML5 server-sent event support, and every other async tool for the
web has an awful server interface.</p>
</div>
<div id="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'grimoire'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
<div id="footer">
<p>
The Codex —
Powered by <a href="http://markdoc.org/">Markdoc</a>.
<a href="https://bitbucket.org/ojacobson/grimoire.ca/src/master/wiki/dev/stop-building-synchronous-web-containers.md">See this page on Bitbucket</a> (<a href="https://bitbucket.org/ojacobson/grimoire.ca/history-node/master/wiki/dev/stop-building-synchronous-web-containers.md">history</a>).
</p>
</div>
</div>
</body>
</html>
|