0

I have a Python app that needs to fetch data from 2-3 different sources (SQL Server, MongoDB etc..) and it can be done in parallel, as I simply need all of the data together later, and each request does not rely on the others.

I couldn't figure which is better for this case - threads, processes or async await?

I read that differences are mostly in CPU usage and I/O. But what if I simply wish to make multiple requests simultaneously (and not sequentially)? Of course, no CPU usage here at all.

1
  • 1
    use threads if you do not have async libs for the tasks, otherwise use asyncio. There is no need to use multiprocessing module for I/O tasks, so you have only twio choices in Python to accomplish the task. Commented Aug 4, 2021 at 16:06

1 Answer 1

1

I'd suggest you have a look at python's builtin Threading module...

... a quote from the doc:

CPython implementation detail: In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing or concurrent.futures.ProcessPoolExecutor. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously.

in short... multithreading has the problem of GIL (e.g. global interpreter lock) but as you can read here...

...the GIL is always released when doing I/O.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.