threads, processes, and co-routines

Source: Internet
Author: User
Tags semaphore

One, thread

First thread

 Import Threading   # importing thread module def  F1 (ARG):    Print  = Threading. Thread (target=f1,args= (123))    # defines a thread task, object F1, incoming parameter 123T.start ()       # Perform thread Tasks

Basic use

Threading is used to provide thread-related operations, which are the smallest unit of work in an application.

More ways:

Start thread is ready to wait for CPU scheduling

SetName setting a name for a thread

GetName Get thread Name

Setdaemon set to background thread or foreground thread (default)
If it is a background thread, during the main thread execution, the background thread is also in progress, and after the main thread finishes executing, the background thread stops regardless of success or not.
If it is the foreground thread, during the main thread execution, the foreground thread is also in progress, and after the main thread finishes executing, wait for the foreground thread to finish, the program stops

The join executes each thread one by one and continues execution after execution, making multithreading meaningless

The Run method that executes the thread object automatically after the run thread is dispatched by the CPU

Custom Threads

ImportThreading#classMyThread (Threading. Thread):#Inheriting threading Methods    def __init__(Self, Func, args):#redefining the Init methodSelf.func =func Self._args=args Super (MyThread, self).__init__()#Execute a custom init method    defRun (self): Self.func (Self._args)defF2 (ARG):#Custom Tasks    Print(ARG) obj= MyThread (F2, 123,) Obj.start ()

Thread Lock

ImportThreadingImportTimenum= 10deffunc (L):GlobalNUM#lockedl.acquire () NUM-= 1Time.sleep (2)    Print(NUM)#Unlockl.release () lock= Threading. Lock ()#One-time#lock = Threading. Rlock () #支持多次, multiple locks forIinchRange (10): T= Threading. Thread (target=func,args=(lock,)) T.start ()

Signal Volume (Semaphore)

mutexes allow only one thread to change data at the same time, while Semaphore allows a certain number of threads to change data, such as a toilet with 3 pits, which allows up to 3 people to go to the toilet, while the latter can only wait for someone to come out.


Custom thread pool

ImportQueue#Import QueueImportThreading#Import ThreadImportTime#Import TimeclassThreadPool:def __init__(Self, maxsize=5):#define the default maximum of 5 thread tasksSelf.maxsize =maxsize self._q=queue. Queue (maxsize) forIinchRange (maxsize): Self._q.put (Threading. Thread)#"Threading. Thread,threading. Thread,threading. Thread,threading. Thread,threading. Thread "    defGet_thread (self):#Get Queue Task        returnSelf._q.get ()defAdd_thread (self):#Increase ThreadSelf._q.put (Threading. Thread) Pool= ThreadPool (5)#instantiation ofdefTask (ARG,P):#Defining Task Methods    Print(ARG) time.sleep (1) P.add_thread () forIinchRange (100):#Let's say we set 100 missions.    #Threading. Thread Classt =pool.get_thread () obj= T (target=task,args= (I,pool,))#Defining TasksObj.start ()#Start a task        

Signal Volume

ImportThreading,timedefrun (N): Semaphore.acquire ()#Mutual exclusion LockTime.sleep (1)    Print("run the thread:%s"%N) semaphore.release ()if __name__=='__main__': Num=0 Semaphore= Threading. Boundedsemaphore (5)#allow up to 5 threads to run at a time     forIinchRange (20): T= Threading. Thread (target=run,args=(i,)) T.start ()

Events (Event)

The events of the Python thread are used by the main thread to control the execution of other threads, and the event provides three methods set, wait, clear.

Event handling mechanism: A global definition of a "flag", if the "flag" value is False, then when the program executes the Event.wait method is blocked, if the "flag" value is true, then the Event.wait method will no longer block.

Clear: Set "Flag" to False

Set: Sets "Flag" to True

ImportThreadingdeffunc (i, E):Print(i) e.wait ()Print(i + 100) Event=Threading. Event () forIinchRange (10): T= Threading. Thread (Target=func, args=(I,event,)) T.start () event.clear ( )#set to red, stopINP= Input ('>>>>')ifINP = ="1": Event.set ()#set to green, perform

Timer

Timer, specifying n seconds after an action is performed

 from Import Timer def Hello ():     Print ("Hello,World"= Timer (1, hello) t.start ()  # After 1 seconds, "Hello, World" would be printed

Producer consumer Model (queue)


Ii. process

 fromMultiprocessingImportProcess fromMultiprocessingImportQueuesImportMultiprocessingdeffoo (i,arg): Arg.put (i)Print('say hi', I,arg.qsize ())if __name__=="__main__":    #li = []Li = queues. Queue (20,ctx=multiprocessing) forIinchRange (10): P= Process (target=foo,args=(I,li,))#P.daemon = TrueP.start ()#P.join ()

The default data is not shared, and you can use the following three methods for process data sharing

Queues

 fromMultiprocessingImportProcess fromMultiprocessingImportQueuesImportMultiprocessingdeffoo (i,arg): Arg.put (i)Print('say hi', I,arg.qsize ())if __name__=="__main__":    #li = []Li = queues. Queue (20,ctx=multiprocessing) forIinchRange (10): P= Process (target=foo,args=(I,li,))#P.daemon = TrueP.start ()#P.join ()


Array

From multiprocessing import Process
From multiprocessing import Array
From multiprocessing import Rlock
Import time

def foo (I,LIS,LC):
Lc.acquire ()
Lis[0] = lis[0]-1
Time.sleep (1)
Print (' Say hi ', lis[0])
Lc.release ()

if __name__ = = "__main__":
# li = []
Li = Array (' i ', 1)
Li[0] = 10
Lock = Rlock ()
For I in range (10):
p = Process (target=foo,args= (I,li,lock))
P.start ()

p = Process (target=foo,args= (I,li,lock))
P.start ()

Manager.dict

 fromMultiprocessingImportProcess fromMultiprocessingImportManagerdeffoo (i,arg): Arg[i]= i + 100Print(Arg.values ())if __name__=="__main__": obj=Manager () Li=obj.dict () forIinchRange (10): P= Process (target=foo,args=(I,li,)) P.start () P.join ( )


Process Pool

 fromMultiprocessingImportPoolImport TimedefF1 (ARG):Print(ARG,'b') Time.sleep (5)    Print(ARG,'a')if __name__=="__main__": Pool= Pool (5)     forIinchRange (30):        #pool.apply (func=f1,args= (i,))Pool.apply_async (func=f1,args=(i,))#Pool.close () # All the tasks are doneTime.sleep (2) pool.terminate ()#Immediate terminationPool.join ()


Ps:
IO-intensive-multithreading
Compute-intensive-multi-process

Third, the co-process


Principle: Use a thread to decompose a thread into multiple "micro-Threading" = = Program Levels
Greenlet

Importgeventdeffoo ():Print('Running in Foo') gevent.sleep (0)Print('Explicit context Switch to Foo again') defBar ():Print('Explicit context to bar') gevent.sleep (0)Print('implicit context switch back to bar') Gevent.joinall ([Gevent.spawn (foo), Gevent.spawn (bar),])


Gevent Installation:

PIP3 Install Gevent

threads, processes, and co-routines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.