Skip to content

Commit 6300ba2

Browse files
committed
Publish new article
1 parent b698972 commit 6300ba2

File tree

46 files changed

+869
-303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+869
-303
lines changed

all/index.html

+52-51
Original file line numberDiff line numberDiff line change
@@ -43,107 +43,108 @@ <h1><strong style="font-size:35">$sam_frances</strong>->web_development($program
4343

4444
<aside id="featured">
4545
<article>
46-
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2018/May/28/now-9/">What I'm doing now</a></h2>
46+
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2018/Jun/28/using-generators-to-build-async-agnostic-api-clients/">Using generators to build async agnostic API clients</a></h2>
4747
<footer class="blog_post_dateline">
48-
<span>Mon 28 May 2018</span>
49-
50-
</footer><!-- /.post-info --><p><em>(This is my <a href="http://nownownow.com/about">now page</a>. It tells you what I'm doing at the moment. It also reminds <strong>me</strong> what I have decided I want to be doing at the moment.)</em></p>
51-
<p>Reading:</p>
52-
<ul>
53-
<li><del><a href="https://pragprog.com/book/cdc-elixir/learn-functional-programming-with-elixir">Learn Functional Programming with Elixir</a>
54-
by Ulisses Almeida</del> (finished)</li>
55-
<li>&#43; <a href="http://shop.oreilly.com/product/0636920030584.do">Introducing Elixir</a> by Simon St. Laurent and J. Eisenberg</li>
56-
<li>&#43; <a href="http://www.obeythetestinggoat.com/">Test-Driven Development with Python, 2nd Edition</a> by Harry J.W. Percival</li>
57-
<li><del><a href="http://shop.oreilly.com/product/0636920032502.do">Lightweight Django</a> by Julia Elman and Mark Lavin</del> (finished)</li>
58-
</ul>
59-
<p>Looking forward to reading:</p>
60-
<ul>
61-
<li><a href="https://www.manning.com/books/amazon-web-services-in-action">Amazon Web Services in Action</a> by Michael Wittig &amp;
62-
Andreas Wittig</li>
63-
<li><a href="https://leanpub.com/javascriptallongesix">JavaScript Allongé, the "Six" Edition</a></li>
64-
<li><a href="https://bleedingedgepress.com/developing-a-redux-edge/">Developing a Redux Edge</a> by Johannes Lumpe, Karl Purkhardt,
65-
Art Muller, Darío Cravero &amp; Ezekiel Chentnik</li>
66-
<li><a href="https://www.manning.com/books/docker-in-action">Docker in Action</a> by Jeff Nickoloff</li>
67-
<li><a href="https://www.manning.com/books/rabbitmq-in-depth">RabbitMQ in Depth</a> by Gavin M. Roy</li>
68-
<li><a href="https://www.nostarch.com/howlinuxworks2">How Linux Works</a> by Brian Ward</li>
69-
<li><a href="http://www.apress.com/la/book/9781430258544">Foundations of Python Network Programming</a> by Brandon Rhodes</li>
70-
<li><a href="http://zguide.zeromq.org/page:all">ZeroMQ</a> by Pieter Hintjens</li>
71-
<li><a href="https://www.ansiblefordevops.com/">Ansible for DevOps</a> by Jeff Geerling</li>
72-
<li><a href="http://haskellbook.com/">Haskell programming from first principles</a> by Christopher Allen and Julie Moronuki</li>
73-
<li><a href="https://www.amazon.co.uk/Foundation-Mathematics-K-Stroud/dp/0230579078/">Foundation Mathematics</a> by K.A. Stroud</li>
74-
<li><a href="http://www.headfirstlabs.com/books/hfalg/">Head First Algebra</a> by Tracy Pilone &amp; Dan Pilone</li>
75-
<li><a href="http://www.librarything.com/catalog.php?view=samfrances&amp;collection=-1&amp;shelf=list&amp;sort=dateread">So many books!!!</a></li>
76-
</ul>
77-
<p>Studying:</p>
78-
<ul>
79-
<li><a href="https://www.edx.org/micromasters/software-development">UBCx: Software Development MicroMasters</a> on <a href="https://www.edx.org">Edx</a></li>
80-
</ul>
81-
<p>Working on:</p>
82-
<ul>
83-
<li>Lots of interesting stuff for <a href="https://cydarmedical.com/">Cydar</a>, involving things like Django Rest Framework, Celery, Ansible, and AWS</li>
84-
</ul> </article>
48+
<span>Thu 28 June 2018</span>
49+
<span>| tags: <a href="https://samfrances.co.uk/tag/python.html">python</a>, <a href="https://samfrances.co.uk/tag/asyncio.html">asyncio</a>, <a href="https://samfrances.co.uk/tag/async.html">async</a></span>
50+
</footer><!-- /.post-info --><p>I'm a big fan of asyncio, couroutines and <code>async</code>/<code>await</code> syntax in
51+
Python 3.5+. However, they come with some well documented downsides. Not least
52+
among these is the
53+
<a href="http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/">red/blue function problem</a>.
54+
Coroutines can call plain functions, but only a coroutine can await another
55+
coroutine (unless you count running it by directly invoking the event loop).
56+
Meanwhile, if a coroutine calls a plain function which triggers some sort of
57+
blocking IO, the whole event loop is blocked.</p>
58+
<p>I have found the red/blue divide particularly annoying in the context of web
59+
API clients. Take <a href="https://boto3.readthedocs.io/">boto3</a>, the popular client for
60+
AWS's various APIs. This only works in a synchronous context. Another project,
61+
<a href="https://github.com/aio-libs/aiobotocore">aiobotocore</a>, provides some of these
62+
capabilities in an async context, but doesn't support all services and operations.</p>
63+
<p>Yet, many of the things API clients do have nothing directly to do with IO,
64+
async or otherwise. Preparing a HTTP request - determining what headers to
65+
use, constructing a request body, calculating signatures and other
66+
authentication details - is a purely functional, IO-free business.
67+
The same goes for interpreting a HTTP response.</p>
68+
<p>A function or method in an API client is like a sandwich where constructing
69+
the request and interpreting the response are the two pieces of bread, and sending
70+
the request is the filling. We ought to be able to use the same bread for any
71+
type of sandwich, without caring if the filling is ham, cheese, or something else.</p>
72+
<p>Ok, enough with the shaky metaphors. Time for some code.</p>
73+
<p>The strategy I have stumbled upon is to write the pure
74+
request-construction / response-interpretation logic as a generator function,
75+
which yields in the middle. The generator yields an object providing details of
76+
the HTTP request it wishes to be sent, ceding control to "something else, I care not
77+
what" which executes the actual web request, and sends an object representing the response
78+
back into the generator. The generator then processes the response.</p>
79+
<p>Here's a toy API client for <a href="http://www.icndb.com/">The Internet Chuck Norris Database</a>.</p>
80+
<script src="https://gitlab.com/samfrances/async-agnostic-api-client-example/snippets/1729032.js"></script>
81+
82+
<p>I still haven't decided if this is a good strategy overall, although it
83+
certainly seems to achieve its immediate goal of allowing a large chunk of
84+
code to be shared between sync and async clients. Comments welcome.</p>
85+
<p>¡Hasta pronto!</p> </article>
8586
</aside><!-- /#featured -->
8687
<section id="other_articles">
8788
<h2>Other articles</h2>
8889
<ol id="posts-list" class="hfeed">
8990

9091
<li><article class="hentry">
9192
<header>
92-
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2018/Mar/28/now-8/" rel="bookmark"
93+
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2018/May/28/now-9/" rel="bookmark"
9394
title="Permalink to What I'm doing now">What I'm doing now</a></h2>
9495
</header>
9596

9697
<div class="entry-content">
9798
<footer class="blog_post_dateline">
98-
<span>Wed 28 March 2018</span>
99+
<span>Mon 28 May 2018</span>
99100

100101
</footer><!-- /.post-info --> <p>What I'm doing now</p>
101-
<p><a class="readmore" href="https://samfrances.co.uk/posts/2018/Mar/28/now-8/">read more</a></p>
102+
<p><a class="readmore" href="https://samfrances.co.uk/posts/2018/May/28/now-9/">read more</a></p>
102103
</div><!-- /.entry-content -->
103104
</article></li>
104105

105106
<li><article class="hentry">
106107
<header>
107-
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Dec/11/now-7/" rel="bookmark"
108+
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2018/Mar/28/now-8/" rel="bookmark"
108109
title="Permalink to What I'm doing now">What I'm doing now</a></h2>
109110
</header>
110111

111112
<div class="entry-content">
112113
<footer class="blog_post_dateline">
113-
<span>Mon 11 December 2017</span>
114+
<span>Wed 28 March 2018</span>
114115

115116
</footer><!-- /.post-info --> <p>What I'm doing now</p>
116-
<p><a class="readmore" href="https://samfrances.co.uk/posts/2017/Dec/11/now-7/">read more</a></p>
117+
<p><a class="readmore" href="https://samfrances.co.uk/posts/2018/Mar/28/now-8/">read more</a></p>
117118
</div><!-- /.entry-content -->
118119
</article></li>
119120

120121
<li><article class="hentry">
121122
<header>
122-
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Nov/19/now-6/" rel="bookmark"
123+
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Dec/11/now-7/" rel="bookmark"
123124
title="Permalink to What I'm doing now">What I'm doing now</a></h2>
124125
</header>
125126

126127
<div class="entry-content">
127128
<footer class="blog_post_dateline">
128-
<span>Sun 19 November 2017</span>
129+
<span>Mon 11 December 2017</span>
129130

130131
</footer><!-- /.post-info --> <p>What I'm doing now</p>
131-
<p><a class="readmore" href="https://samfrances.co.uk/posts/2017/Nov/19/now-6/">read more</a></p>
132+
<p><a class="readmore" href="https://samfrances.co.uk/posts/2017/Dec/11/now-7/">read more</a></p>
132133
</div><!-- /.entry-content -->
133134
</article></li>
134135

135136
<li><article class="hentry">
136137
<header>
137-
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Apr/24/now-5/" rel="bookmark"
138+
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Nov/19/now-6/" rel="bookmark"
138139
title="Permalink to What I'm doing now">What I'm doing now</a></h2>
139140
</header>
140141

141142
<div class="entry-content">
142143
<footer class="blog_post_dateline">
143-
<span>Mon 24 April 2017</span>
144+
<span>Sun 19 November 2017</span>
144145

145146
</footer><!-- /.post-info --> <p>What I'm doing now</p>
146-
<p><a class="readmore" href="https://samfrances.co.uk/posts/2017/Apr/24/now-5/">read more</a></p>
147+
<p><a class="readmore" href="https://samfrances.co.uk/posts/2017/Nov/19/now-6/">read more</a></p>
147148
</div><!-- /.entry-content -->
148149
</article></li>
149150
</ol><!-- /#posts-list -->
@@ -160,7 +161,7 @@ <h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Apr/2
160161
<h3>Tags</h3>
161162

162163
<p>
163-
<a href="https://samfrances.co.uk/tag/node.html">node</a>, <a href="https://samfrances.co.uk/tag/pelican.html">pelican</a>, <a href="https://samfrances.co.uk/tag/canvas.html">canvas</a>, <a href="https://samfrances.co.uk/tag/design-patterns.html">design-patterns</a>, <a href="https://samfrances.co.uk/tag/python.html">python</a>, <a href="https://samfrances.co.uk/tag/javascript.html">javascript</a>, <a href="https://samfrances.co.uk/tag/aws.html">aws</a>, <a href="https://samfrances.co.uk/tag/django.html">django</a>, <a href="https://samfrances.co.uk/tag/ansible.html">ansible</a>, <a href="https://samfrances.co.uk/tag/html5.html">html5</a>, <a href="https://samfrances.co.uk/tag/tutorials.html">tutorials</a> </p>
164+
<a href="https://samfrances.co.uk/tag/node.html">node</a>, <a href="https://samfrances.co.uk/tag/pelican.html">pelican</a>, <a href="https://samfrances.co.uk/tag/canvas.html">canvas</a>, <a href="https://samfrances.co.uk/tag/design-patterns.html">design-patterns</a>, <a href="https://samfrances.co.uk/tag/python.html">python</a>, <a href="https://samfrances.co.uk/tag/javascript.html">javascript</a>, <a href="https://samfrances.co.uk/tag/aws.html">aws</a>, <a href="https://samfrances.co.uk/tag/django.html">django</a>, <a href="https://samfrances.co.uk/tag/ansible.html">ansible</a>, <a href="https://samfrances.co.uk/tag/html5.html">html5</a>, <a href="https://samfrances.co.uk/tag/tutorials.html">tutorials</a>, <a href="https://samfrances.co.uk/tag/async.html">async</a>, <a href="https://samfrances.co.uk/tag/asyncio.html">asyncio</a> </p>
164165

165166
<h3>Feeds</h3>
166167

all/index2.html

+16-16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ <h1><strong style="font-size:35">$sam_frances</strong>->web_development($program
4343

4444
<section id="other_articles">
4545
<ol id="posts-list" class="hfeed" start="4">
46+
<li><article class="hentry">
47+
<header>
48+
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Apr/24/now-5/" rel="bookmark"
49+
title="Permalink to What I'm doing now">What I'm doing now</a></h2>
50+
</header>
51+
52+
<div class="entry-content">
53+
<footer class="blog_post_dateline">
54+
<span>Mon 24 April 2017</span>
55+
56+
</footer><!-- /.post-info --> <p>What I'm doing now</p>
57+
<p><a class="readmore" href="https://samfrances.co.uk/posts/2017/Apr/24/now-5/">read more</a></p>
58+
</div><!-- /.entry-content -->
59+
</article></li>
60+
4661
<li><article class="hentry">
4762
<header>
4863
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Feb/14/now-4/" rel="bookmark"
@@ -102,21 +117,6 @@ <h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Jan/3
102117
<p><a class="readmore" href="https://samfrances.co.uk/posts/2017/Jan/30/now-1/">read more</a></p>
103118
</div><!-- /.entry-content -->
104119
</article></li>
105-
106-
<li><article class="hentry">
107-
<header>
108-
<h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Jan/28/now-0/" rel="bookmark"
109-
title="Permalink to What I'm doing now">What I'm doing now</a></h2>
110-
</header>
111-
112-
<div class="entry-content">
113-
<footer class="blog_post_dateline">
114-
<span>Sat 28 January 2017</span>
115-
116-
</footer><!-- /.post-info -->
117-
<p><a class="readmore" href="https://samfrances.co.uk/posts/2017/Jan/28/now-0/">read more</a></p>
118-
</div><!-- /.entry-content -->
119-
</article></li>
120120
</ol><!-- /#posts-list -->
121121
<p class="paginator">
122122
<a href="https://samfrances.co.uk/all/index.html">&laquo;</a>
@@ -132,7 +132,7 @@ <h2 class="blog_post_heading"><a href="https://samfrances.co.uk/posts/2017/Jan/2
132132
<h3>Tags</h3>
133133

134134
<p>
135-
<a href="https://samfrances.co.uk/tag/node.html">node</a>, <a href="https://samfrances.co.uk/tag/pelican.html">pelican</a>, <a href="https://samfrances.co.uk/tag/canvas.html">canvas</a>, <a href="https://samfrances.co.uk/tag/design-patterns.html">design-patterns</a>, <a href="https://samfrances.co.uk/tag/python.html">python</a>, <a href="https://samfrances.co.uk/tag/javascript.html">javascript</a>, <a href="https://samfrances.co.uk/tag/aws.html">aws</a>, <a href="https://samfrances.co.uk/tag/django.html">django</a>, <a href="https://samfrances.co.uk/tag/ansible.html">ansible</a>, <a href="https://samfrances.co.uk/tag/html5.html">html5</a>, <a href="https://samfrances.co.uk/tag/tutorials.html">tutorials</a> </p>
135+
<a href="https://samfrances.co.uk/tag/node.html">node</a>, <a href="https://samfrances.co.uk/tag/pelican.html">pelican</a>, <a href="https://samfrances.co.uk/tag/canvas.html">canvas</a>, <a href="https://samfrances.co.uk/tag/design-patterns.html">design-patterns</a>, <a href="https://samfrances.co.uk/tag/python.html">python</a>, <a href="https://samfrances.co.uk/tag/javascript.html">javascript</a>, <a href="https://samfrances.co.uk/tag/aws.html">aws</a>, <a href="https://samfrances.co.uk/tag/django.html">django</a>, <a href="https://samfrances.co.uk/tag/ansible.html">ansible</a>, <a href="https://samfrances.co.uk/tag/html5.html">html5</a>, <a href="https://samfrances.co.uk/tag/tutorials.html">tutorials</a>, <a href="https://samfrances.co.uk/tag/async.html">async</a>, <a href="https://samfrances.co.uk/tag/asyncio.html">asyncio</a> </p>
136136

137137
<h3>Feeds</h3>
138138

0 commit comments

Comments
 (0)