Python concurrent multi-process, multi-threading, coprocessor, and asynchronous

Source: Internet
Author: User

One, multi-threaded

Second, the association process (also known as micro-threading, fiber)

The co-process, unlike a thread's preemptive scheduling, is a collaborative dispatch. The process can be implemented in Python by generator.

The first step is to have a solid understanding of the generator and yield.

Calling a normal Python function is usually performed from the first line of the function, ending with a return statement, exception, or function execution (which can also be considered implicitly returning none).

Once the function returns control to the caller, it means that it is all over. Sometimes you can create a function that produces a sequence to "save your own work," which is the generator (a function that uses the yield keyword).

  The ability to "produce a sequence" is because the function does not return as usual. Return implies that the function is returning control of the execution code to the place where the function was called. The implied meaning of "yield" is that the transfer of control is temporary and voluntary, and our function will regain control in the future.

For details, see: Explaining ' yield ' and ' generators ' (generator)

III. Multi-process

1, sub-process (subprocess package)

In Python, fork a child process through the subprocess package, and run an external program.

The OS module is the first to be considered when invoking the system's commands. Use Os.system () and Os.popen () to operate. However, these two commands are too simple to perform complex operations, such as providing input to a running command or reading the output of a command, judging the running state of the command, managing the parallelism of multiple commands, and so on. In this case, the Popen command in Subprocess will be able to perform the operation we need effectively.

>>>Import  subprocess>>>command_line=-C www.baidu.com> >>args=shlex.split (command_line)>>>p=subprocess. Popen (args)

Use subprocess. Pipes connect the inputs and outputs of multiple sub-processes together to form a pipeline (pipe):

Import= subprocess. Popen (["ls","-L"],stdout= = subprocess. Popen (["WC"], stdin=child1.stdout,stdout== child2.communicate ()  Print(out)

The communicate () method reads the data from STDOUT and stderr and enters it into stdin.

Python concurrent multi-process, multi-threading, coprocessor, and asynchronous

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.