Threading.local
在多线程环境下,每个线程都有自己的数据。一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁。
Threading.local可以创建一个对象,每个线程都可以对他读写属性,但不会互相影响
import threading
import time
# 创建全局ThreadLocal对象:
class A:
pass
# local_school = A()
local_school = threading.local() def process_student():
print('Hello, %s (in %s)' % (local_school.student, threading.current_thread().name)) def process_thread(name):
# 绑定ThreadLocal的student: local_school.student = name
time.sleep(2)
process_student()
a = []
for i in range(20):
a.append(threading.Thread(target= process_thread, args=(str(i),), name=str(i))) for i in a:
i.start()
通过字典以及面向对象中的魔法方法来自己实现一个
import time
from threading import get_ident,Thread
class PPP:
def __init__(self):
object.__setattr__(self,"storage", {})
def __setattr__(self, key, value):
if get_ident() in self.storage:
self.storage[get_ident()][key]=value
else:
self.storage[get_ident()] ={key: value}
def __getattr__(self, item):
return self.storage[get_ident()][item] p =PPP()
def task(arg):
p.a = arg
time.sleep(2)
print(p.a) for i in range(10):
t = Thread(target=task,args=(i,))
t.start()
Threading.local的更多相关文章
- 自定义threading.local
1.threading相关. # Author:Jesi # Time : 2018/12/28 14:21 import threading import time from threading i ...
- threading.local学习
多线程抢占问题 import time import threading obj = 5 def task(arg): global obj obj = arg time.sleep(1) print ...
- 网络编程 多线程/socketserver模块/ threading.local
线程:进程中负责程序执行的执行单元. 多线程:在1个进程中存在多个线程. 进程只是用来把资源集中在一起,而线程才是cpu上的执行单位. 每个进程都会默认有一个控制线程也叫作主线程. 进程之间是竞争关系 ...
- 多线程局部变量之threading.local()用法
假如,开了十个线程并且做同样的一件事,他们需要带着自己的数据进来,完成事情后带着自己的数据出去.如果是并发,同时进来,他们的数据就会混乱. 一般情况,我们加锁就可以了,一个人先进来,先加锁,另一个人过 ...
- python之threading.local
简述: threading.local是全局变量但是它的值却在当前调用它的线程当中 作用: 在threading module中,有一个非常特别的类local.一旦在主线程实例化了一个local,它会 ...
- [Python 多线程] threading.local类 (六)
在使用threading.local()之前,先了解一下局部变量和全局变量. 局部变量: import threading import time def worker(): x = 0 for i ...
- threading.local()方法;线程池
一,threading.local() import time import threading v = threading.local() def func(arg): # 内部会为当前线程创建一个 ...
- 多线程threading.local的作用及原理?
1.示例代码 import time import threading v = threading.local() def func(arg): # 内部会为当前线程创建一个空间用于存储:phone= ...
- 线程锁、threading.local(flask源码中用的到)、线程池、生产者消费者模型
一.线程锁 线程安全,多线程操作时,内部会让所有线程排队处理.如:list/dict/Queue 线程不安全 + 人(锁) => 排队处理 1.RLock/Lock:一次放一个 a.创建10个线 ...
随机推荐
- Expressions versus statements in JavaScript
Statements and expressions An expression produces a value and can be written wherever a value is exp ...
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- PHP7 学习笔记(十二)PHPExcel vs PhpSpreadsheet and PHP_XLSXWriter
前言 PhpSpreadsheet是PHPExcel的下一个版本. 它打破了兼容性,极大地提高了代码库的质量(命名空间,PSR合规性,使用最新的PHP语言功能等).由于所有努力都转移到了PhpSpre ...
- 归并排序_JAVA
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] a = { 6, ...
- 简述react与vue的区别
React 和Vue是现在主流的两个框架(相对来说angular用的已经少了) 两者的区别体现在以下方面 相同点: 1.react和vue都支持服务端渲染 2.都有虚拟DOM,组件化开发,通过prop ...
- CSS3 transform-origin 属性
<!DOCTYPE html> <html> <head> <style> #div1 { position: relative; height: 20 ...
- Java实现猜数字,附带提示功能。
很简单的一段代码: package com.changeyd.demo; import java.util.Random;import java.util.Scanner;public class M ...
- (5)top k大的数目
一.问题 在一个很长的数组中,求出top k大小的数目 二.办法 用优先队列 时间复杂度O(nlog(k)),应该是最差的情况下是这个 三.Code package algorithm; import ...
- 第27月第6天 gcd timer
1.gcd timer 因为如果不用GCD,编码需要注意以下三个细节: 1.必须保证有一个活跃的runloop. performSelector和scheduledTimerWithTimeInter ...
- GDI+学习---1.初识GDI+
---恢复内容开始--- GDI+: GDI+由一组C++类实现,是对于GDI的继承,GDI+不仅优化了大部分GDI性能而且提供了更多特性.允许应用程序开发者将信息显示在显示器或者打印机上,而无需考虑 ...