DAY 94 flask03

不用local

from threading import Thread

import time

#

a = -1

b = 2

#

def task(arg):

global a

a = arg

time.sleep(2)

print(a)

# #

for i in range(10):

t = Thread(target=task, args=(i,))

t.start()

from threading import Thread from threading import local import time

""" {'线程1':{'value':1},'线程2':{'value':1},'线程3':{'value':1},'线程4':{'value':1},'线程5':{'value':1}}

""" from threading import get_ident

特殊的对象

lqz = local()

def task(arg):

# 对象.val = 1/2/3/4/5

lqz.value = arg

time.sleep(2)

print(lqz.value)

for i in range(10):

t = Thread(target=task,args=(i,))

t.start()

"""

local对象只支持线程,不支持协程,生成自己的local对象,各改个的,互补影响 {'线程1':{'value':1},'线程2':{'value':1},'线程3':{'value':1},'线程4':{'value':1},'线程5':{'value':1}}

"""

面向过程写法

from threading import get_ident,Thread,current_thread

import time

storage = {}

def set(k,v):

# 获取线程id

ident = get_ident()

if ident in storage:

storageident = v

else:

storage[ident] = {k:v}

def get(k):

ident = get_ident()

return storageident

def task(arg):

set('val',arg)

v = get('val')

print(v)

#

for i in range(10):

t = Thread(target=task,args=(i,))

t.start()

面向对象写法

from threading import get_ident, Thread

import time

# #

class Local(object):

storage = {}

#

def set(self, k, v):

ident = get_ident()

if ident in Local.storage:

Local.storageident = v

else:

Local.storage[ident] = {k: v}

#

def get(self, k):

ident = get_ident()

return Local.storageident

# #

obj = Local()

# #

def task(arg):

obj.set('val', arg)

v = obj.get('val')

print(v)

# #

for i in range(10):

t = Thread(target=task, args=(i,))

t.start()

# #

from threading import get_ident, Thread

import time

# #

class Local(object):

storage = {}

#

def setattr(self, k, v):

ident = get_ident()

if ident in Local.storage:

Local.storageident = v

else:

Local.storage[ident] = {k: v}

#

def getattr(self, k):

ident = get_ident()

return Local.storageident

# #

obj = Local()

# # #

def task(arg):

# setattr

# getattr

obj.val = arg

print(obj.val)

# #

for i in range(10):

t = Thread(target=task, args=(i,))

t.start()

from threading import get_ident, Thread import time

#

class Local(object):

def init(self):

object.setattr(self, 'storage', {})

# self.setattr('storage', {})

#

def setattr(self, k, v):

ident = get_ident()

if ident in self.storage:

self.storageident = v

else:

self.storage[ident] = {k: v}

#

def getattr(self, k):

ident = get_ident()

return self.storageident

# #

obj = Local()

# #

def task(arg):

obj.val = arg

obj.xxx = arg

print(obj.val)

# #

for i in range(10):

t = Thread(target=task, args=(i,))

t.start()

try: # getcurrent 获取协程id from greenlet import getcurrent as get_ident except Exception as e: # from threading import get_ident

from threading import Thread import time

class Local(object): def init(self): object.setattr(self, 'storage', {})

def __setattr__(self, k, v):
  ident = get_ident()
  if ident in self.storage:
      self.storage[ident][k] = v
  else:
      self.storage[ident] = {k: v}

def __getattr__(self, k):
  ident = get_ident()
  return self.storage[ident][k]

obj = Local()

def task(arg): obj.val = arg obj.xxx = arg print(obj.val)

for i in range(10): t = Thread(target=task, args=(i,)) t.start()

posted @ 2021-06-17 16:40  DEJAVU_ERIC  阅读(20)  评论(0编辑  收藏  举报