3 ms·
> Summary if cpu_intensive: 'processes' else: if suited_for_threads: 'threads' elif suited_for_asyncio: 'asyncio' Interesting take
by whilenot-dev 2y ago
> Summary
if cpu_intensive:
'processes'
else:
if suited_for_threads:
'threads'
elif suited_for_asyncio:
'asyncio'
Interesting takeaway! For web services mine would be:
1. always use asyncio
2. use threads with asyncio.to_thread[0] to convert blocking calls to asyncio with ease
3. use aiomultiprocess[1] for any cpu intensive tasks
[0]: https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread https://docs.python.org/3/library/asyncio-task.html#asyncio....
[1]: https://aiomultiprocess.omnilib.dev/en/stable/ https://aiomultiprocess.omnilib.dev/en/stable/
- d0mine 2y agoCommon in practice variant: don't use pure Python for cpu-intensive tasks (offload to C extensions)
- eternityforest 2y agoAnd those C extensions usually release the GIL, so you can do real multithreading to some extent.