Python Threading.Lock() threading.thread(target=lambda:[()])

import uuid
import threading
from datetime import datetime
import time

idx=0
idx_lock=threading.Lock()

def get_time_uuid():
    with idx_lock:
        global idx
        idx=idx+1
        current_idx=idx
        return f'{current_idx}_{datetime.now().strftime('%Y%m%d%H%M%S%f')}_{uuid.uuid4().hex}'

def get_uuid_time():
    with idx_lock:
        global idx
        idx=idx+1
        current_idx=idx
        return f'{current_idx}_{uuid.uuid4().hex}_{datetime.now().strftime('%Y%m%d%H%M%S%f')}'


t1=threading.Thread(target=lambda:[
    (
        print(get_time_uuid()),
        print(get_uuid_time()),
        print(a),
        time.sleep(1)
    )for a in range(1,101)
])

t1.start()
t1.join()

print(f'{datetime.now()} thread id:{threading.current_thread().ident}')

 

 

 

image

 

posted @ 2025-12-23 20:38  FredGrit  阅读(2)  评论(0)    收藏  举报