Old Boys video --flask thread, coroutine

request

(1) single-threaded process: certainly no problem in the form of import. Based on global variables do
(2) a single multi-threaded process: After the server up and running, the client requests over 1, with a thread to handle it, the client requests over 2, with another thread to handle. Handle simultaneous requests, if the value of a global variable with the request, any thread can change the value of the request. threading.local objects, open up a space for each thread to preserve its unique value, data storage (data isolation). threading.local can open up space for each thread is through the principle that uniquely identifies the thread to do. If you want i support coroutine, it is necessary to obtain a unique identification coroutine by greenlet.
(3) single-threaded process (one thread can open multiple coroutines to share resources) threading.local objects can not. . For the client's request may be processed to create a coroutine. If the process coroutines encounter IO requests, allowing the client to request another coroutine continue processing,

Note: (1) if they support coroutine, with threading.local the objects
(2) if supported coroutine, you can customize similar objects threading.local

Coroutine

  1. You can get a unique identification thread:

_Thread Import get_ident from
get_ident () # call this function can get unique identification

  1. Coroutine can get a unique identification:

from greenlet import getcurrent as get_ident

Set and read value of the property

class Foo(object):

    def __setattr__(self, key, value):
        print(key, value)

    def __getattr__(self, item):
        print(item)


obj = Foo()
obj.z = 300
obj.w

Custom objects of similar threading.local

from greenlet import getcurrent as get_ident
防止反复调用 生成递归
class Local(object):
	def __init__(self):
		object.__setattr__(self, 'storage',{})
        # self.storage = {}				# 设置值
        
	def __setattr__(self, key, value):
        print(key, value)

If you call an object method .xx will trigger __setattr__

  def __setattr__(self, key, value):
        print(key, value)

Guess you like

Origin blog.csdn.net/xili2532/article/details/91450648