Sobes.tech
Junior — Middle

What types of multitasking exist and how are they implemented?

sobes.tech AI

Answer from AI

There are several types of multitasking:

  1. Process multitasking — simultaneous execution of multiple processes, each with its own address space.
  2. Thread multitasking — execution of multiple threads within a single process, sharing memory.
  3. Cooperative multitasking — switching between tasks occurs at the initiative of the tasks themselves.
  4. 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())