Is `await` in Python3 collaborative multitasking?

I am trying to understand the new async coroutines (introduced in Python 3.5).

In 1997, I went to university, which roughly covered the contents of Andrew Tanenbaum’s book Modern Operating Systems .

Somehow awaitin Python3, Joint multitasking reminds me .

From Wikipedia:

Collaborative multitasking, also known as uncaught multitasking, is a computer multitasking style in which the operating system never initiates a context switch from a running process to another process. Instead, processes voluntarily give control periodically or in standby mode to simultaneously launch multiple applications. This type of multitasking is called “cooperative,” because all programs must interact in order for the entire planning scheme to work.

If you look at the Python interpreter as an operating system, is the term "Collaborative multitasking" applicable await?

But maybe I'm missing something.

+4
3

coroutine . , ().

, . , , , , . . .

+5

.

, . asyncio.sleep , time.sleep . , , id .

import threading
import asyncio
import time

async def async_function(i):
    started = time.time()
    print("Id:", i, "ThreadId:", threading.get_ident())
    await asyncio.sleep(1)
    time.sleep(1)
    print("Id:", i, "ThreadId:", threading.get_ident(), "Time:", time.time() - started)

async def async_main():
    await asyncio.gather(
        async_function(1),
        async_function(2),
        async_function(3)
    )

loop = asyncio.get_event_loop()
loop.run_until_complete(async_main())

:

Id: 3 ThreadId: 140027884312320
Id: 2 ThreadId: 140027884312320
Id: 1 ThreadId: 140027884312320
Id: 3 ThreadId: 140027884312320 Time: 2.002575397491455
Id: 2 ThreadId: 140027884312320 Time: 3.0038201808929443
Id: 1 ThreadId: 140027884312320 Time: 4.00504469871521

. . asyncio.sleep(1) , 1 , . time.sleep(1) ( ), . Id 1 id 2, id 2 id 3.

# async/await , ?

#:

using System;
using System.Threading;
using System.Threading.Tasks;

namespace AsyncTest
{
    class MainClass {
        private static async Task AsyncMethod(int id) {
            var started = DateTime.Now;
            Console.WriteLine("Id: {0} ThreadId: {1}", id, Thread.CurrentThread.ManagedThreadId);
            await Task.Delay(1000);
            Thread.Sleep(1000);
            Console.WriteLine("Id: {0} ThreadId: {1} Time: {2}", id, Thread.CurrentThread.ManagedThreadId, DateTime.Now - started);
        }

        private static async Task MainAsync()
        {
            await Task.WhenAll(AsyncMethod(1), AsyncMethod(2), AsyncMethod(3));
        }

        public static void Main (string[] args) {
            MainAsync().Wait();
        }
    }
}

...

Id: 1 ThreadId: 1
Id: 2 ThreadId: 1
Id: 3 ThreadId: 1
Id: 2 ThreadId: 7 Time: 00:00:02.0147000
Id: 3 ThreadId: 8 Time: 00:00:02.0144560
Id: 1 ThreadId: 6 Time: 00:00:02.0878160

. . 2 ! ?

. Python, async/await # . Task.Delay(1000) , , , , , Thread.Sleep(1000) .

, # , (, SynchronizationContext), .

+4

. Wikipedia:

Coroutines are computer software components that generalize routines for opaque multitasking , allowing multiple entry points to pause and resume execution in specific places.

+3
source

Source: https://habr.com/ru/post/1650757/


All Articles