Python concurrent programming understands yield from, co-process

Source: Internet
Author: User
Tags iterable

First, home, meet the iteration, iterators and generators

Can iterate, such as: List,dict,tuple,deque and so on are the iterative object;

Validation requires the use of the COLLECTIONS.ABC module (not in Python2) to class whether an object is an iterative (iterable) or an iterator (Iterator) using Isinstance (). Whether it is a generator (Generator)

The code is as follows: Import collections

From COLLECTIONS.ABC import Iterable.Iterator.Generator

#字符串

Ast= "Xiaoming"

Print ("string: {}". Format (AST))

Print (Isinstarnce (ast,iterable))

Print (Isinstance (ast,iterator))

Print (Isinstance (alist,generator))

#列表

ALIST=[21,24,12,32,19]

Print (Isinstarnce (alist,iterable))

Print (Isinstance (alist,iterator))

Print (Isinstance (alist,generator))

#字典

adict={"name": "Wang Hua", "gender": "Male", "Age": 18}

Print (Isinstarnce (adict,iterable))

Print (Isinstance (adict,iterator))

Print (Isinstance (adict,generator))

#deque

Adeque=collections.deque ("Adbdef")

Print ("string: {}". Format (Adeque))

Print (Isinstarnce (adeque,iterable))

Print (Isinstance (adeque,iterator))

Print (Isinstance (adeque,generator))

Extended Knowledge:

An iterative object, which is implemented internally, __iter__ this magic method. The Dir () method can be used to see if a __iter__ is available to determine whether a variable is iterative;

Iterators:

An iterator is actually a function that is more than an iterative object. is __next__ (), we can no longer use the For loop to get the element value intermittently. Instead, it can be implemented directly using the next () method.

Iterators are implemented on the basis of an iterative iteration. To create an iterator, we first have to have an iterative object.

* * Focus: Generator

The concept of a generator is the first occurrence in python2.2, and the idea of introducing a generator is to implement a structure that does not need to waste space when calculating the next value. The generator is based on an iterator that can use the for loop to achieve yield again.

What is yield, which is equivalent to the return in our function, yields the new value back at each next () or for traversal, and blocks here, waiting for the next call. It is this mechanism that uses generators to shine in Python programming.

The life cycle of the generator:

Gen_creaated #等待开始执行

Gen_running #解释器正在执行 (This state is only visible in multi-threaded applications)

gen_suspended #在yield表达式处暂停

Gen_closed #执行结束

* * co-process

A process is a computer program component that produces subroutines for non-preemptive multitasking, allowing different entry points to pause or start executing programs at different locations.

By using yield pause generator, the process can pass the execution of the program to the other subroutines, thus realizing the alternating execution between different subroutines.

Python concurrent programming understands yield from, co-process

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.