Python------Thread

Source: Internet
Author: User

First, we know that whether it's creating multiple processes or creating multithreaded pools to solve problems, it takes time to create processes, create threads, and manage transitions between them.

Concurrency is implemented based on a single thread, which saves the time it takes to create a thread process.

Second, how to achieve the switch between the two functions?

def func1 ():     Print (1)     yield    Print (3)     yield def Func2 ():     = func1 ()    Next (g)    print(2)    next (g)    print(4  ) Func2 ()"1234"
Toggle 1
defconsumer (): whiletrue:n=yield        Print('consumed a bun of%s'%N)defproducer (): G=consumer () Next (g) forIinchRange (5):        Print('produced the bun%s'%i) g.send (i) producer ()" "production of buns 0 consumed a bun 0 produced a bun 1 consumed a bun 1 produced a bun 2 consumed a bun 2 produced a bun 3 consumption of a bun 3 produced a bun 4 consumed a bun 4" "
Toggle 2
Import Timedefconsumer ():" "Task 1: Receive data, process data" "     whiletrue:x=yielddefproducer ():" "Task 2: Production data" "g=consumer () Next (g) forIinchRange (10000000): G.send (i) Time.sleep (2) Start=Time.time () producer ( )#concurrent execution, but the task producer encounters an IO block and does not cut into other tasks within that thread to executeStop=time.time ()Print(Stop-start)
yield does not meet IO blocking

For single-threaded, IO operations are unavoidable in the program, but if we can control multiple tasks under single thread in our own program (that is, at the user program level, not the operating system level), you can switch to another task to calculate when another task encounters io blocking. This ensures that the thread is in the best possible state, that it can be executed at any time by the CPU, and that we can hide our IO operations to the maximum extent possible at the user program level, thus confusing the operating system to see that the thread seems to have been calculating, the IO is relatively small, This allows more CPU execution permissions to be allocated to our threads.

Third, the co-process

Co-process: is a single-threaded concurrency, the association is a user-state lightweight thread, that is, the association process is controlled by the user program itself.

The nature of the process: under the thread, the user controls a task by itself when the IO is blocked, the other task is switched to execute, in order to improve efficiency.

It should be emphasized that:

1:python threads are at the kernel level, which is controlled by the operating system (such as a single-threaded encounter with IO or too long execution time will be forced to hand over the CPU execution permissions, switch other threads to run).

2: Single-wire range on, once IO is encountered, the switch is controlled from the application level (not the operating system) to improve efficiency (non-IO switching is not efficiency-independent)

Compared with the operating system control thread switching, the user in a single-threaded control of the transition of the pros and cons:

Advantages:

1. The transition cost of the association is smaller, it belongs to the program-level switch, the operating system is not fully aware, and therefore more lightweight.

2. The concurrency can be achieved within a single thread to maximize CPU utilization.

Disadvantages:

1. The nature of the process is single-threaded, unable to take advantage of multi-core, can be a program to open multiple processes, each in-process open multiple threads, each line range to open the co-path.

2. The association refers to a single thread, so once the association is blocked, the entire thread is blocked.

Features of the co-process:

1. Concurrency must be implemented in only one single thread

2. No lock required to modify shared data

3. The context stack in the user program that holds multiple control flows

4. Add: A co-process encountered IO operation automatically switch to other co-process (how to implement detection Io,yield, Greenlet can not be achieved, the use of the Gevent module (select mechanism))

Four, Greenlet module

 fromGreenletImportGreenletdefEat (name):Print('%s Eat 1'%name) G2.switch ('haha')    Print('%s Eat 2'%name) G2.switch ()defPlay (name):Print('%s Play 1'%name) G1.switch ()Print('%s Play 2'%name) G1=Greenlet (Eat) G2=Greenlet (play) G1.switch ('HJM')#parameters can be passed in at the first switch, and will not be required at any later time" "HJM eat 1haha play 1HJM eat 2haha play 2" "
Greenlet implementing State switching
Import Time fromGreenletImportGreenlet#modules that switch state in a single threaddefeat1 ():Print('eating chicken legs 1') G2.switch () Time.sleep (5)    Print('Eat chicken wings 2') G2.switch ()defeat2 ():Print('Eat dumplings 1') G1.switch () Time.sleep (3)    Print('White cut Chicken') G1=Greenlet (EAT1) G2=Greenlet (EAT2) g1.switch ()" "eat chicken leg 1 eat dumplings 1 eat chicken wings 2 white cut chicken" "
Greenlet implementing State switching 2

A simple switchover (in the absence of an IO or an operation that does not re-open the memory space) will slow down the execution of the program.

#Sequential ExecutionImport TimedefF1 (): Res=1 forIinchRange (100000000): Res+=IdefF2 (): Res=1 forIinchRange (100000000): Res*=Istart=Time.time () F1 () F2 () Stop=time.time ()Print('run time is%s'% (Stop-start))#run time is 10.494175910949707#Toggle fromGreenletImportGreenletImport TimedefF1 (): Res=1 forIinchRange (100000000): Res+=i g2.switch ()defF2 (): Res=1 forIinchRange (100000000): Res*=I G1.switch () Start=time.time () G1=Greenlet (F1) G2=Greenlet (F2) G1.switch () Stop=time.time ()Print('run time is%s'% (Stop-start))#run time is 63.0725622177124
Efficiency comparison

Greenlet just provides a more convenient way to switch than generator, when cutting to a task execution if you encounter Io, then blocking in place, still does not solve the problem of automatically switching to the IO to improve efficiency.

Five, Gevent module

Python------Thread

Related Article

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.