threads, processes, and co-routines

Source: Internet
Author: User
Tags for in range thread stop

Treading is used to provide thread-related operations, and threads are the smallest unit of work in an application

#!/usr/bin/env python#Coding:utf-8ImportThreadingImport TimedefShow (ARG): Time.sleep (1)    Print 'Thread'+Str (ARG) forIinchRange (10): T= Threading. Thread (target=show,args=(i,)) T.start ()Print 'Main thread Stop'" "Print Result: Main thread stopthread0thread5thread4thread1thread2 thread3thread7thread6thread8thread9" "

The above code creates 10 processes, then the controller is handed over to CPU,CPU according to the specified algorithm for scheduling, shard execution instructions. The appearance of the disorder is normal, as they both preempt the screen at the same time.

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
      Run method that executes the thread class object after the run thread is dispatched by the CPU

Thread Lock

Since the threads are randomly dispatched, and each thread may execute only n execution, the CPU then executes the other threads. So there might be a resource preemption like the above to preempt the screen output, so the thread lock appears

#!/usr/bin/env python#Coding:utf-8ImportThreadingImportTimenum=0 Lock=Threading. Rlock ()defFun ():#LockingLock.acquire ()GlobalNum Time.sleep (1)    PrintNum#Unlocklock.release () forIinchRange (10): T= Threading. Thread (target=Fun ,) t.start () Print output:1-10 in order

Event

The events of the Python thread are used for the main thread to control the execution of other threads, and the events mainly provide three methods, set, wait, clear

Event handling mechanism: 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 Flase

Set: Sets Flag to True

#!/usr/bin/env python# coding:utf-8import threadingdef Do (event):    print ' Start '    # Wait Method    event.wait ()    print ' execute ' EVENT_ONJ = Threading. Event () for I in range:    t = Threading. Thread (target=do,args= (Event_onj,))    T.start () # set flag=falseevent_onj.clear () InP = raw_input (' input: ') if inp = = ' True ':    # set Flag=true    event_onj.set ()

Execution of the above code: first all 10 processes are executed to print ' start ', then Event_onj.clear () is executed, the state is set to false, in the waiting state, when I enter true, the Event_onj.set () is executed, The status has been set to true. Continue to execute down

Here is the result of the execution:

Startstartstartstartstartstartstartstartstartstartinput:trueexecuteexecuteexecuteexecuteexecute Executeexecute
Executeexecuteexecute

Use of multithreading

Create a thread

 #  !/usr/bin/env python  #   coding:utf-8  from  multiprocessing import   Process  Span style= "COLOR: #0000ff" >import   threading  import   time  def   do (i):  print   " say hi   " ,i  for  i in  range (2  = Process (Target=do,args= (I,)) P.start () 
      

Note: Because the data between processes needs to be held separately, the creation process requires very large overhead (process cannot be created under Windows)

Process data sharing

The process holds one piece of data, and the data is not shared by default

Sharing mode one: Array

#!/usr/bin/env python#Coding:utf-8 fromMultiprocessingImportProcess,array#Create an array that contains only numeric types (list)#The number of arrays is immutabletemp = Array ('I', [1,2,3,4,5,6])defFoo (i): Temp[i]= 100+I forIteminchTemp:PrintI'=======', Item forIinchRange (2): P= Process (target=foo,args=(i,)) P.start ()

Sharing Mode II: MANAGE.DICT

# !/usr/bin/env python # Coding:utf-8  from Import            = = def Foo (i): = 100+ i print    for in range (2):    = Process (target=foo,args=(i,))    P.start ()    P.join ()

Types of shared data

'C': Ctypes.c_char,'u': Ctypes.c_wchar,'b': Ctypes.c_byte,'B': Ctypes.c_ubyte,'h': Ctypes.c_short,'H': Ctypes.c_ushort,'I': Ctypes.c_int,'I': Ctypes.c_uint,'L': Ctypes.c_long,'L': Ctypes.c_ulong,'F': Ctypes.c_float,'D': ctypes.c_double

When the process is created (when not in use), the shared data is taken to the child process, and is then assigned to the original value when the process finishes executing.

You can also use process locks here

#!/usr/bin/env python#-*-coding:utf-8-*-from multiprocessing import Process, Array, Rlockdef Foo (lock,temp,i): "    " "Add    No. 0 Number" "" "    Lock.acquire ()    temp[0] = 100+i for    item in temp:        print I, '-----> ', item    lock.release () lock = Rlock () temp = Array (' i ', [one, one, all, and]) for I in range:    p = Process (target=foo,args= (l Ock,temp,i,))    P.start ()

Process Pool

A process sequence is maintained internally by the process pool, and when used, a process is fetched in the process pool, and the program waits until a process is available in the process pool sequence if there are no incoming processes available for use.

There are two methods in a process pool:

1:apply go to the process pool to request a process

2:apply_async go to the process pool to request a process

#!/usr/bin/env python#-*-coding:utf-8-*- fromMultiprocessingImportProcess,poolImport TimedefFoo (i): Time.sleep (2)    returni+100#The arg here is the return value of executing the Foo function.defBar (ARG):PrintArg#Create five processesPool = Pool (5)PrintPool.apply (Foo, (1,))PrintPool.apply (Foo, (2,))PrintPool.apply (Foo, (3,))PrintPool.apply (Foo, (4,))PrintPool.apply (Foo, (5,))#after one of the above five finishes, there is a spare process after which the sixth process is executed. PrintPool.apply (Foo, (6,)) forIinchRange (10):    #Loop executes 10 processes, and after each process executes, the bar in the callback method is executedPool.apply_async (Func=foo, args= (i,), callback=Bar)Print 'End'pool.close () pool.join ( )#When the process in the process pool finishes executing and then shuts down, the program shuts down if commented. 

Co-process

The operation of the thread and process is triggered by the program to trigger the system interface, the final performer is the system, and the operation of the coprocessor is the programmer.

The significance of the existence of the process: for multi-threaded applications, the CPU by slicing the way to switch between threads of execution, thread switching takes time (save state, next continue). , only one thread is used, and a code block execution order is specified in one thread.

Application scenario: When there are a large number of operations in the program that do not require the CPU (IO), it is suitable for the association process;

Plainly, it is the programmer's execution of the program.

The process is a shard of the thread

The greenlet here need to be installed

#!/usr/bin/env python#-*-coding:utf-8-*-from  greenlet import Greenlet  def test1 ():    print 12
    # Switch to coprocessor 2 to execute    gr2.switch ()    print    gr2.switch ()  def test2 ():    Print 56
    # Switch to coprocessor 1 to execute    gr1.switch ()    Print Gr1 = Greenlet (test1) GR2 = Greenlet (test2) Gr1.switch ()

This execution is a bit like yield.

It is more suitable to use the co-process when the IO operation is very special. Do not use the co-process to calculate many cases.

Gevent

#!/usr/bin/env python#-*-coding:utf-8-*-from gevent import Monkey; Monkey.patch_all () Import geventimport urllib2# import urllib# request www.baidu.com# t = urllib.urlopen ('/HTTP/ Www.baidu.com ') # print t.read () def f (URL):    print (' GET:%s '% URL)    # send an HTTP request    resp = urllib2.urlopen (URL) C3/>data = Resp.read ()    print ('%d bytes received from%s. '% (len (data), URL)) Gevent.joinall ([        # Here is three threads, there is no blocking, Execute the F function, who returns, who then executes        gevent.spawn (f, ' https://www.python.org/'),        gevent.spawn (F, ' https://www.yahoo.com/'),        gevent.spawn (F, ' https://github.com/'),])

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.