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()

  • 相关阅读:
    Python的词法分析与语法分析
    使用svn log确定分支创建的时间点
    Python的作用域
    SVN的版本日期
    理解SVN关键词BASE,HEAD,COMMITTED,PREV
    SVN的属性
    EL表达式 (详解)
    java jsp el fn
    java防止表单重复提交
    数据库的事务处理
  • 原文地址:https://www.cnblogs.com/DEJAVU888/p/14894165.html
Copyright © 2011-2022 走看看