Y
Y
Yermek2020-05-31 08:30:16
Python
Yermek, 2020-05-31 08:30:16

How to process an array of data asynchronously?

Hello. I understand asynchronous programming. Tell me how to properly process the data array?

I only see this way (see # async process):

import asyncio
from asyncio import TimeoutError
import time


async def some_function(input: str, index):
    await asyncio.sleep(4)
    print('func finished')
    input[index] = f'New value of {input[index]}'
    return input[index]


async def main():
    # 'sync' process
    print(time.strftime('%X'))
    await asyncio.sleep(1)
    print("hello")
    await asyncio.sleep(1)
    print('world')
    print(await some_function(['input value'], 0))
    print(time.strftime('%X'))

    # async process
    data = ['input 1', 'input 2', 'input 3', 'input 4', 'input 5']

    tasks = []
    tasks.append(asyncio.create_task(some_function(data, 0)))
    tasks.append(asyncio.create_task(some_function(data, 1)))
    tasks.append(asyncio.create_task(some_function(data, 2)))
    tasks.append(asyncio.create_task(some_function(data, 3)))
    tasks.append(asyncio.create_task(some_function(data, 4)))
    try:
        await asyncio.wait_for(asyncio.gather(*tasks), 5)
        for element in data:
            print(f'Result: {element}')
    except TimeoutError:
        print("Timeout (")

    print(time.strftime('%X'))



asyncio.run(main())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-05-31
@ermek6

1. can be done on queues https://docs.python.org/3/library/asyncio-queue.ht...
2. can be done via pools https://docs.python.org/3/library/asyncio- eventloo
. ;) Well, often data is used for intermediate storage and there is no point in changing it, and in some_function you can immediately pass an element, not its index

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question