Asyncio is like black magic to me. The rules of the language change too much. I don't really believe it's even needed, because we already have threading in the standard library.
Asyncio is like black magic to me. The rules of the language change too much. I don't really believe it's even needed, because we already have threading in the standard library.
import requests
def main():
with requests.Session() as s:
r = s.get('https://...')
result = r.json()
main()
Async, using aiohttp: import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as s:
async with s.get('https://...') as r:
result = await r.json()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
If you're making many HTTP requests, you can use async, queue them up and it greatly improves performance.See also https://docs.aiohttp.org/en/stable/http_request_lifecycle.ht...
Trio solves a problem that threading does not: switching between tasks only happens at deterministic points (they don't run in parallel), and tasks are in a hierarchy so that exceptions are handled in reasonable ways (and you can do a lot of other associated cool things like cancel an entire tree of tasks).
Python has a GIL, so you're not getting parallel Python code anyway without multiprocessing.
This means that if I'm writing code that implements, like, a basic network server, by far the easiest way to do it is Trio. Unlike with threading (in Python or any other language) I don't have to worry about locks from concurrent access / tasks switching at unexpected points, and unlike with e.g. the select module, handling both directions of communication and multiple connections is doable with normal straight-line code instead of weird state machines.
It's not needed, yes, but neither is Python itself. You could just write C.
> It's not needed, yes, but neither is Python itself. You could just write C.
I'm currently learning C, so I can use Python as a front-end for C, giving users a nice interface while using C to handle the hard work, which is what lots of Python libraries do under the hood.
If C is not your thing, there are some interesting projects to use other languages with Python [https://github.com/PyO3/PyO3] | [https://github.com/yglukhov/nimpy]
I must admit that writing in C gives a more fun challenge.
Unfortunately, people these days like more quantity than quality. Look at Rust. People love Rust. There are others nice, fast and safe languages out there, but Rust has one thing other do not: features, features and features. More and more features every new version.