Junior — Middle
What types of multitasking exist and how are they implemented?
sobes.tech AI
Answer from AI
There are several types of multitasking:
- Process multitasking — simultaneous execution of multiple processes, each with its own address space.
- Thread multitasking — execution of multiple threads within a single process, sharing memory.
- Cooperative multitasking — switching between tasks occurs at the initiative of the tasks themselves.
- Preemptive multitasking — the operating system forcibly switches tasks.
In Python, multitasking is implemented through:
- Multithreading — multiple threads within a single process.
- Multiprocessing — multiple processes.
- Asynchronous programming (asyncio) — managing tasks using an event loop.
Example of using asyncio:
import asyncio
async def say_hello():
print('Hello')
await asyncio.sleep(1)
print('World')
asyncio.run(say_hello())