1 threads
1.1 Basic applications
1.1.1 Standard thread (common)
Import Threadingdef F1 (ARG): print (ARG) T = Threading. Thread (TARGET=F1, args= (123,)) T.start ()
1.1.2 Custom Threads
Custom thread classes are both threading. Thread process, customizing the Run method
Import Threadingclass MyThread (threading. Thread): #自定义类, inherit threading. Thread Class def __init__ (self, Func, args): Self.func = func Self.args = args super (MyThread, self). __init __ () def run (self): #定义run方法 Self.func (Self.args) def F2 (ARG): print (arg) obj = MyThread (F2, 123) Obj.start ()
1.2 Queue queues
1.2.1 Queue type
- Queue. Queue advanced First Out queues
- Queue. Lifoqueue LIFO Queue
- Queue. Priorityqueue Priority Queue
- Queue.deque bidirectional queue
1.2.2 Queue. Queue advanced First Out queues
Parameters
put Drop Data (default blocking), whether blocking, time-out when blocking
Get fetch data (default blocking), whether blocking, time-out when blocking
queue. Queue (n), n queues maximum length
empty () # Check if the queue is empty
qsize () # Real number
maxsize () # maximum number of supported
join () # after all the tasks in the queue have been completed (the element is not completed (removed))
Task_done () # End Value Task
Import Queueq = queue. Queue (3) # define queues, up to 10 data q.put (one) # put hold data q.put (x) q.put (33,block=false, timeout=2) # Default block blocked, Timeout Timeout print (q.qsize ()) # Number of real data print (Q.empty ()) #查看队列是否为空print (Q.get ()) Q.task_done () # End-of-value task print (Q.get ()) Q.task_done () #结束取值任务print (Q.get (Block=false, timeout=2)) # Default block blocking, timeout timeout q.task_ Done () #结束取值任务print (Q.empty ()) #查看队列是否为空q. Join () # block the process, use with Task_done () when the task in the queue is no longer blocked
1.2.3 LIFO queue
Q = queue. Lifoqueue () q.put (123) Q.put (456) q.put (789) print (Q.get ()) # 789# 456# 123
1.2.4 Priority Queue
Q = queue. Priorityqueue () Q.put ((1, "alex4")) Q.put ((1, "alex3")) Q.put ((2, "Alex2")) Q.put ((0, "alex0")) Q.put ((1, "Alex1")) print (Q.get ()) print (Q.get ()) print (Q.get ()) print (Q.get ()) # (0, ' alex0 ') # (1, ' Alex1 ') # (1, ' Alex3 ') # (1, ' alex4 ')
PS : If the priority level ID same, sort by parameter
1.2.5 bidirectional queue
Q = Queue.deque () q.append (123) Q.append (456) q.appendleft (789) print (Q.pop ()) print (Q.popleft ()) # 456# 789
1.3 Producer Consumer models (application of queues)
Import queueimport threadingimport timeq = queue. Queue () def productor (ARG): "" " buy tickets :p Aram ARG: : Return:" "" q.put (str (ARG) + ' ticket ') for I in Range: t = Threading. Thread (Target=productor, args= (i,)) T.start () def consumer (ARG): "" " server background :p Aram Arg: : return: "" and True: print (ARG, q.get ()) Time.sleep (2) for J in Range (3): t = Threading. Thread (Target=consumer, Args= (J,)) T.start ()
1.4-wire Lock (Lock,rlock)
1.5 Signal Volume ()
1.6 Events (Event)
1.7 Pieces (Condition)
1.8 Timer
1.9 Thread Pool
Chapter 2nd Progress
2.1 Basic use
2.2 Process data sharing
2.2.1 Queues (queue) data sharing
2.2.2 Array (array)
2.2.3 Manager.dict (dictionary)
2.3 Process Lock
2.4 Process Pool
2.5 Summary
3rd Chapter co-process
3.1 Greenlet
3.2 gevent
3.3 Windows installation Gevent
Importqueue
Q= queue. Queue (3) # define a queue for up to ten data
Q.put ( One) # putStoring data
Q.put ( A)
Q.put ( -,Block=False, Timeout=2) # default block blocking, timeout time-out
Print(Q.qsize ())# Number of real data
Print(Q.empty ())#to see if the queue is empty
Print(Q.get ())
Q.task_done ()#End Value Task
Print(Q.get ())
Q.task_done ()#End Value Task
Print(Q.get (Block=False, Timeout=2)) # default block blocking, timeout time-out
Q.task_done ()#End Value Task
Print(Q.empty ())#to see if the queue is empty
Q.join ()# block the process, use with task_done () when the task in the queue is finished and no longer blocks
11 Thread Process coprocessor