How to compute things in parallel in Python
I had a loop that could be parallelized and wanted to take advantage of my CPU cores.
A quick search led me to https://docs.python.org/3/library/asyncio-task.html#running-tasks-concurrently. It looked simple enough: define your methods as async
, then run them with a gather
. However, I realized that the computation still used only one core and was not faster.
It turns out concurrency is not the same as parallelism. The solution seems to use threading or multiprocessing, but I haven't verified it yet.