#8 – Concurrency and Parallelism Flashcards
(13 cards)
What is concurrency in Python?
Concurrency means running multiple tasks at the same time conceptually, often by switching between them.
What is parallelism in Python?
Parallelism means actually running tasks simultaneously, typically using multiple CPU cores.
What is the difference between concurrency and parallelism?
Concurrency is about structure (tasks that can be paused/resumed), while parallelism is about simultaneous execution.
What is multithreading in Python?
Running multiple threads within the same process. Good for I/O-bound tasks.
What limits Python threads from true parallelism?
The Global Interpreter Lock (GIL) allows only one thread to execute Python bytecode at a time.
What is multiprocessing in Python?
It runs multiple processes, each with its own Python interpreter and memory. It bypasses the GIL.
When should you use multithreading?
Use it for I/O-bound tasks like network calls or file I/O.
When should you use multiprocessing?
Use it for CPU-bound tasks that need true parallel execution.
What is asynchronous programming?
A way to run tasks concurrently using async
and await
without blocking the main thread.
What module is used for asynchronous code?
Use the asyncio
module for writing asynchronous programs.
How do you define an async function?
Use async def
.
async def fetch_data(): return await get_data()
What are await
and async
used for?
async
marks a function as asynchronous, and await
pauses until an async task completes.
Can you mix threading and asyncio?
Yes, but with care. Use loop.run_in_executor()
to run blocking code in threads.