码迷,mamicode.com
首页 > 编程语言 > 详细

python多线程安全local()

时间:2019-01-14 14:41:23      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:range   add   local   rom   current   rgs   静态变量   创建   安全   

import time
import threading
from threading import local

class Foo(local):
    pass
# 实例化(创建)对象
foo = Foo()
# 给Foo类添加静态变量
def add(i):
    foo.num = i
    time.sleep(1)
    # threading.current_thread().ident 线程PID
    print(foo.num, i, threading.current_thread().ident)


for i in range(20):
    # 开启多线程
    th = threading.Thread(target=add, args=(i,))
    th.start()

 

python多线程安全local()

标签:range   add   local   rom   current   rgs   静态变量   创建   安全   

原文地址:https://www.cnblogs.com/apollo1616/p/10266111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!