Foreplay: Understand the next queue queues before you make a custom thread pool
The queue can hold the underlying data type, or it can hold special data types such as classes, objects, etc.
fromQueue Import Queueclasst:def __init__ (self,num): Self.num=num def printf (self): print (Self.num,id (self.num))if__name__ = ="__main__": Queue= Queue (0) Num= A queue.put (num) #可以存放基础数据类型t=T (num) queue.put (t) #可以存放对象CLS=T queue.put (CLS) #可以存放类DT= Queue.Get() print (ID (dt), dt,type (DT)) #1385649280 A<class 'int'>DT= Queue.Get() print (ID (dt), dt,type (DT)) #7652128<__main__. TObjectAt0x000000000074c320> <class '__main__. T'>DT= Queue.Get() print (ID (dt), dt,type (DT)) #18042264<class '__main__. T'> <class 'type'>
The functions that the thread pool should have:
- Create a thread pool first,
- And then to get the data, if you have to take it directly,
- If not, you need to block the wait until there is data to arrive
- The thread pool can set the specified size
- To meet the above "blocking" requirements, set the size of the data structure, we can use the queue
Simple version (Low):
Python---Basics review (10) Processes and threads (custom thread pool, context Manager, and use of the association)