[WIP] Fluent Python Learnings
A summary of my learnings while reading Fluent Python.
I've learnt from reading technical books that trying to read all in one shot can be boring and not too useful for memory retention. Reading it when there's interest in that chapter or when I encounter it at work helps keep me engaged in the topic.
Chapter 19 - Concurrency Models With Python
- Talks about the differences between processes, threads and coroutines.
- When running a python programme, the python interpreter spins up a new process. Within this, a thread is spun up for the user programme and GC. All threads within this process share the same GIL, hence it will not take advantage of multi-cores.
- There are libraries which allow you to spin up a new process. This will have its own Python interpreter and GIL. However note that data exchanged between processes needs to be serialisable.
```python
# Threads vs Coroutines: Threads are scheduled pre-emptively. To prevent a thread from holding the GIL for too long, it will be automatically released after 5ms, to allow other threads to run. This means that threads can be suspended at any time. Meanwhile coroutines will continue running until it yields to another coroutine.
async def a():
await asyncio.sleep() # yields control back to the event loop
```
- Coming from a Go background, the above approaches is very different from Go's concept of Do not communicate by sharing memory; instead, share memory by communicating.' -https://go.dev/blog/codelab-share
![[WIP] Fluent Python Learnings](/_next/image?url=https%3A%2F%2Fimgix.cosmicjs.com%2F8fe270c0-62d6-11ef-b5ae-a594bb4a8e67-1_VZWiDKVUbJsn8kqaH7_Tng.jpg%3Ffit%3Dscale%3Fw%3D1400%26auto%3Dformat&w=3840&q=75)