Python get incremented idx atomically via thread_locking and assign the value to the third variable withing thread lock to ensure thread safety

import time
from datetime import datetime
import threading
import os
import psutil
import uuid
from tkinter import messagebox
import json

idx=0
idx_lock=threading.Lock()

def get_idx():
    global idx
    with idx_lock:
        idx+=1
        # 赋值给局部变量,确保返回的是锁内确定的值
        current_idx=idx
        return current_idx

 

 

import time
from datetime import datetime
import threading
import os
import psutil
import uuid
from tkinter import messagebox
import json

idx=0
idx_lock=threading.Lock()

def get_idx():
    global idx
    with idx_lock:
        idx+=1
        # 赋值给局部变量,确保返回的是锁内确定的值
        current_idx=idx
        return current_idx

def get_memory(): 
    pid=os.getpid()
    proc=psutil.Process(pid)
    mem_info=proc.memory_info()
    rss_mem=f'{mem_info.rss/1024/1024:.2f} M'
    vms_mem=f'{mem_info.vms/1024/1024:.2f} M'

    sys_mem=psutil.virtual_memory()
    total_mem=f'{sys_mem.total/1024/1024/1024:.2f} G'
    avail_mem=f'{sys_mem.available/1024/1024/1024:.2f} G'
    used_percent=f'{sys_mem.percent} %'

    return f'Idx:{idx},{datetime.now()},PId:{pid},rss mem:{rss_mem},vms mem:{vms_mem},total mem:{total_mem},available mem:{avail_mem},used percent:{used_percent}'

class Book:
    def __init__(self,id,name,isbn,abstract,author,comment,content,summary,title,topic):
        self.id=id
        self.name=name
        self.isbn=isbn
        self.abstract=abstract
        self.author=author
        self.comment=comment
        self.content=content
        self.summary=summary
        self.title=title
        self.topic=topic
    
    def to_dict(self):
        return {
            'Id':self.id,
            'Name':self.name,
            'ISBN':self.isbn,
            'Author':self.author,
            'Abstract':self.abstract,
            'Comment':self.comment,
            'Content':self.content,
            'Summary':self.summary,
            'Title':self.title,
            'Topic':self.topic
        }

data_list=[]

arr=range(1,30000001)
for a in arr:
    bk=Book(a,f'Name_{a}',f'ISBN_{a}_{uuid.uuid4().hex}',f'Abstract_{a}',f'Author_{a}',f'Comment_{a}',f'Content_{a}',f'Summary_{a}',f'Title_{a}',f'Topic_{a}')
    data_list.append(bk.to_dict())
    get_idx()

    if a%1000000==0:
        print(f'a:{a},{get_memory()}')


json_file=f'Json_{datetime.now().strftime('%Y%m%d%H%M%S%f')}.json'
with open(json_file,'w',encoding='utf-8-sig') as json_write_file:
    json.dump(data_list,json_write_file,indent=4)

mem_str=get_memory()
print(mem_str)
messagebox.showinfo(datetime.now(),mem_str)

 

 

 

 

 

 

image

 

 

 

 

image

 

 

 

image

 

 

image

 

posted @ 2026-02-23 12:10  FredGrit  阅读(5)  评论(0)    收藏  举报