4 ms·
You have a point. Sadly, they misunderstand what attracts more developers. It's all about the quality, not the quantity of features. Asyncio is like black magi
by wheelerof4te 5y ago
You have a point. Sadly, they misunderstand what attracts more developers. It's all about the quality, not the quantity of features.
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.
- geofft 5y agoAsyncio is weird. But check out Trio, an alternative and easier-to-understand async framework: https://trio.readthedocs.io/en/stable/tutorial.html https://trio.readthedocs.io/en/stable/tutorial.html 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.
- akagusu 5y agoI love Trio, much better than the standard asyncio. > 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/PyO3/PyO3] | [https://github.com/yglukhov/nimpy https://github.com/yglukhov/nimpy]
- wheelerof4te 5y agoPython and C work wonders together. I must admit that writing in C gives a more fun challenge.
- gabrielsroka 5y agoSync, using requests: 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.html https://docs.aiohttp.org/en/stable/http_request_lifecycle.ht...
- gabrielsroka 5y agoHere are some more realistic examples utilizing the official HN API. The async version is about 30 times faster than the sync version when you set the total to a high number, the maximum is about 500. Performance tables are in the code itself. https://github.com/gabrielsroka/hn https://github.com/gabrielsroka/hn See also https://news.ycombinator.com/item?id=30172433 https://news.ycombinator.com/item?id=30172433
- akagusu 5y ago> You have a point. Sadly, they misunderstand what attracts more developers. It's all about the quality, not the quantity of features. 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.