3 ms·
The main problem is that many Python libraries come in only sync flavor and using them together with asyncio is a pain. NodeJS libraries are almost all standar
by dheera 6y ago
The main problem is that many Python libraries come in only sync flavor and using them together with asyncio is a pain.
NodeJS libraries are almost all standardized to async now.
Hell, even Tensorflow.js can give you Promises of computation results.
- heavyset_go 6y agoSeems pretty painless to me: $ python3 -m asyncio >>> from requests import get >>> url = 'https://example.com' >>> response = await asyncio.to_thread(get, url) A lot of Python libraries now offer async APIs, have async support on their short term roadmaps, or other async projects have replaced them.
- dayjah 6y agooh neat! I did not know about asyncio.to_thread; I've been using the longer form thread executor pattern.
- simonw 6y agoI hadn't seen that either - looks like it's new in Python 3.9: https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread https://docs.python.org/3/library/asyncio-task.html#asyncio....
- heavyset_go 6y agoAnd if you aren't using 3.9, the anyio package has a run_async() function in its to_thread module[1] that works the same way as asyncio's to_thread() coroutine function, and the anyio solution is compatible with both asyncio and trio. [1] https://anyio.readthedocs.io/en/stable/threads.html https://anyio.readthedocs.io/en/stable/threads.html