Skip to content

Commit

Permalink
Blogpost about python async
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsimper committed Dec 20, 2024
1 parent eadbfc8 commit 5429229
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/blog/posts/_data.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[
{
"slug": "learning-async-in-python",
"title": "Learning async in Python",
"date": "Fri Dec 20 2024 09:04:25 GMT+0100 (Central European Standard Time)",
"tags": []
},
{
"slug": "managing-python-dependencies-with-uv",
"title": "Managing Python dependencies with UV",
Expand Down
31 changes: 31 additions & 0 deletions app/blog/posts/learning-async-in-python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Learning async in Python

Coming from Node.js async code is the norm, and when async/await was introduced a big shift happened since it was competing with the callback approach where you could archive the same.

In Python it is not the same, Python is by default sync and webservers relies on WSGI (Web Server Gateway Interface) like [gunicorn](https://docs.gunicorn.org/en/stable/) to handle multiple connections on a server.

So python tutorials does not normally talk or show async code examples, so learning it in python is defenitely harder.

## The asyncio package

This package includes a lot of the functionality on how to control flow in your now async code. This blog shows really well the different methods like `async.create_task`, ` asyncio.wait`, `asyncio.gather` and how to use them.

[https://jacobpadilla.com/articles/handling-asyncio-tasks](https://jacobpadilla.com/articles/handling-asyncio-tasks)

## Different froms of concurrency

With Python being sync by default you also have to consider which kind of concurrency you need. Node.js concurrency is not the solution to all your problems either, since it still will run on a single core.

This blogpost really explains it well, but the conclusion sums up pretty well
[http://masnun.rocks/2016/10/06/async-python-the-different-forms-of-concurrency/](http://masnun.rocks/2016/10/06/async-python-the-different-forms-of-concurrency/)

```python
if io_bound:
if io_very_slow:
print("Use Asyncio")
else:
print("Use Threads")
else:
print("Multi Processing")
```

0 comments on commit 5429229

Please sign in to comment.