Python asyncio.gather returns a future aggregating results from the given coroutines/futures.

Return a future aggregating results from the given coroutines/futures.
Coroutines will be wrapped in a future and scheduled in the event loop. They will not necessarily be scheduled in the same order as passed in.
All futures must share the same event loop. If all the tasks are done successfully, the returned future's result is the list of results (in the order of the original sequence, not necessarily
the order of results arrival). If return_exceptions is True,exceptions in the tasks are treated the same as successful results, and gathered in the result list; otherwise, the first
raised exception will be immediately propagated to the returned future.

 

 

import uuid
from datetime import datetime
import time
import threading
import psutil
import os
import asyncio

idx=0
idx_lock=threading.Lock()

def get_idx():
    global idx
    with idx_lock:
        idx+=1
        current_idx=idx
        return current_idx

def get_time_uuid():
    return f'{get_idx()}_{datetime.now().strftime('%Y%m%d%H%M%S%f')}_{uuid.uuid4().hex}'

def get_uuid_time():
    return f'{get_idx()}_{uuid.uuid4().hex}_{datetime.now().strftime('%Y%m%d%H%M%S%f')}'


async def print_time_uuid():
    print(get_time_uuid())

async def print_uuid_time():
    print(get_uuid_time())

async def asyncio_gather_func():
    while True:
        await asyncio.gather(         
                print_uuid_time(),
                print_time_uuid()                
        )
        await asyncio.sleep(1)

if __name__=='__main__':
    try:
        asyncio.run(asyncio_gather_func())
    except Exception as ex:
        print(f"{datetime.now()},{str(ex)}")

 

 

 

 

image

 

 

 

image

 

posted @ 2026-02-22 21:53  FredGrit  阅读(7)  评论(0)    收藏  举报