<story><title>Write Fast Apps Using Async Python 3.6 and Redis</title><url>https://eng.paxos.com/write-fast-apps-using-async-python-3.6-and-redis</url></story><parent_chain><item><author>Waterluvian</author><text>I get that everyone&amp;#x27;s responses are thinking some big public thing. I&amp;#x27;m thinking small toy implementation for my home network.&lt;p&gt;The toy experiment is how to do what&amp;#x27;s trivial in Node with Python. Mainly because I like working with python. I think the answer might be: Python is the wrong tool for the job.</text></item><item><author>bobbyi_settv</author><text>You normally use something like redis to store the state.&lt;p&gt;If you were going to share state in memory between threads, how would you handle the case where the second request goes to a different server or that the process has restarted? You&amp;#x27;d need redis anyway, so you might as well just use it in all cases.</text></item><item><author>Waterluvian</author><text>I have never found a good example of a Python web server that provides some mechanism for statefulness.  Is it just fundamentally not possible to have shared state among requests handled by the threads of a process?  Sanic&amp;#x27;s examples seem to be the same as Flask&amp;#x27;s: self-contained function calls attached to endpoints.&lt;p&gt;I keep hitting a wall with Python when I want to do something like:&lt;p&gt;1. subscribe to a websocket connection and keep the last received message in state
2. expose an http endpoint to let a client GET that last message.</text></item><item><author>erikcw</author><text>We&amp;#x27;ve just recently started using Sanic[0] paired with Redis to great effect for a very high throughput web service.  It also uses Python 3 asyncio&amp;#x2F;uvloop at its core.  So far very happy with it.&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;channelcat&amp;#x2F;sanic&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;channelcat&amp;#x2F;sanic&lt;/a&gt;</text></item></parent_chain><comment><author>rcarmo</author><text>Erm, no. You can do shared thread storage, in Python, it&amp;#x27;s just that it doesn&amp;#x27;t really scale. I&amp;#x27;ve done it for small daemons without significant hassle, and even wrote my own Go-like CSP helper: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;rcarmo&amp;#x2F;python-utils&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;taskkit.py&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;rcarmo&amp;#x2F;python-utils&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;taskkit.p...&lt;/a&gt;</text></comment>
<story><title>Write Fast Apps Using Async Python 3.6 and Redis</title><url>https://eng.paxos.com/write-fast-apps-using-async-python-3.6-and-redis</url></story><parent_chain><item><author>Waterluvian</author><text>I get that everyone&amp;#x27;s responses are thinking some big public thing. I&amp;#x27;m thinking small toy implementation for my home network.&lt;p&gt;The toy experiment is how to do what&amp;#x27;s trivial in Node with Python. Mainly because I like working with python. I think the answer might be: Python is the wrong tool for the job.</text></item><item><author>bobbyi_settv</author><text>You normally use something like redis to store the state.&lt;p&gt;If you were going to share state in memory between threads, how would you handle the case where the second request goes to a different server or that the process has restarted? You&amp;#x27;d need redis anyway, so you might as well just use it in all cases.</text></item><item><author>Waterluvian</author><text>I have never found a good example of a Python web server that provides some mechanism for statefulness.  Is it just fundamentally not possible to have shared state among requests handled by the threads of a process?  Sanic&amp;#x27;s examples seem to be the same as Flask&amp;#x27;s: self-contained function calls attached to endpoints.&lt;p&gt;I keep hitting a wall with Python when I want to do something like:&lt;p&gt;1. subscribe to a websocket connection and keep the last received message in state
2. expose an http endpoint to let a client GET that last message.</text></item><item><author>erikcw</author><text>We&amp;#x27;ve just recently started using Sanic[0] paired with Redis to great effect for a very high throughput web service.  It also uses Python 3 asyncio&amp;#x2F;uvloop at its core.  So far very happy with it.&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;channelcat&amp;#x2F;sanic&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;channelcat&amp;#x2F;sanic&lt;/a&gt;</text></item></parent_chain><comment><author>zeptomu</author><text>The problem is that accessing shared state concurrently in a multi-process context is a non-trivial problem, so specific software emerged that handles these problems for you.&lt;p&gt;The simplest solution is to use a small DB system like sqlite. It is built into Python (import sqlite3) performs reasonably well and you do not have to run an additional service.&lt;p&gt;Now if a small DB like sqlite already feels overblown to you (and it really &lt;i&gt;is&lt;/i&gt; simple and small) you might not need concurrent access either, so the simplest solution is to just use a file where you store your state.</text></comment>