Summary of the co-process issues for Python

Source: Internet
Author: User

The process is actually a thread that can be controlled autonomously by the program.

In Python, which is dominated by yield and yield from, it is possible to understand the co-process through the creator consumer example

Use yield from to transfer data to the generator (co-process)
# Traditional producer-the consumer is a thread that writes messages, a thread takes messages, controls the queue through a lock mechanism, and waits, but can be accidentally deadlocked.
# If the use of the process, the producer produced the message, directly through the yield jump to the consumer began to execute, until the consumer execution, return to the producer continued production, high efficiency
defconsumer (): R="'     whiletrue:n=yieldRif  notN:return        Print('[CONSUMER] consuming%s ...'%N) R='OK'defProduce (c): C.send (None) n=0 whileN < 5: N= n + 1Print('[PRODUCER] producing%s ...'%N) R=c.send (n)Print('[PRODUCER] Consumer return:%s'%r) c.close () C=consumer () produce (c)
# Note that the consumer function is a generator that puts a consumer into the produce after:
# First Call C.send (None) to start the generator;
# Then, once something has been produced, switch to consumer execution by C.send (n);
# consumer through yield to get the news, processing, and through yield to return the results;
# Produce get the results of consumer processing, continue to produce the next message;
# produce decided not to produce, through C.close () Close the consumer, the entire process is over.
# The entire process is unlocked, executed by one thread, produce and consumer collaborate to complete the task, so called "co-process" rather than the thread's preemptive multitasking.
# finally apply Donald Knuth's sentence to summarize the features of the process:
# "subroutine is a special case of the association process. ”

Understanding the above example is important for Python's understanding of the process

Here's how Python3.4 supports the process

ImportThreadingImportAsyncio@asyncio.coroutinedefHello (s):Print(s)Print('Hello world! (%s)'%Threading.currentthread ())yield  fromAsyncio.sleep (1)    Print(s)Print('Hello again! (%s)'%threading.currentthread ()) Loop=Asyncio.get_event_loop () Tasks= [Hello ('W'), Hello ('e')]loop.run_until_complete (Hello ('o'))#Add to task represents execution togetherLoop.run_until_complete (asyncio.wait (Tasks)) Tasks2= [Hello ('W'), Hello ('e'), Hello ('H')]Print('++++++++++++++++++++') Loop.run_until_complete (asyncio.wait (TASKS2)) Loop.close ()
# is done concurrently by a thread through coroutine.
# Async and await are the new syntax for Coroutine, and to use the new syntax, you only need to do a two-step simple replacement:
# Replace the @asyncio.coroutine with async;
# Replace the yield from the await.
Note the new syntax can only be used in Python 3.5 and later versions, and if you use version 3.4, you still need to use the previous section of the scenario.

Importdef  Hello ():    print("Hello world! " )    = await asyncio.sleep (1)    print("Hello again! " ) Loop=Asyncio.get_event_loop () loop.run_until_complete (hello ())

Summary of the co-process issues for Python

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: 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.