zoukankan      html  css  js  c++  java
  • 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()

  • 相关阅读:
    1058 A+B in Hogwarts (20分)
    我的Vue之小功能统计
    H5如何用Canvas画布生成并保存带图片文字的新年快乐的海报
    微信小程序之特殊效果及功能
    移动端H5适配方法(盒子+图片+文字)
    5分钟教你3种实现验证码功能
    微信小程序动态生成保存二维码
    微信授权获取code(微信支付)
    H5微信自定义分享链接(设置标题+简介+图片)
    带你走近WebSocket协议
  • 原文地址:https://www.cnblogs.com/DEJAVU888/p/14894165.html
Copyright © 2011-2022 走看看