Python popen popen2

Source: Internet
Author: User

Currently, a query program get_user_id is written in C. Python needs to call this program: Use get_user_id "username" to get the output: "id0002451 ".

Using pipelines in Python allows you to conveniently call this process and get its standard output: get_user_id is written in C. The approximate framework of the program is as follows: Get a command line parameter, then print the corresponding id based on the parameter:
Int main (INT argc, char * ARGs [])
{
Char * name = ARGs [1]
Printf ("% s", GETID (name ));
}

The Python program call snippets are as follows: pipe = OS. popen ("get_user_id" + "myname ")

User_id = pipe. Read () pipe. Close ()
Python calls the get_user_id program in this way. First, Python starts a sub-process, reads the standard output of the sub-process, and ends the sub-process. The main overhead here is the process startup and revocation, and the communication between pipelines is very fast. If get_user_id is called less frequently, there is no problem with this call method. If get_user_id is called frequently, the performance of get_user_id becomes a bottleneck of the system, so it is necessary to optimize it. The optimization method is to make the get_user_id sub-process resident memory, the python parent process can use the write/flush and Readline methods to communicate with the sub-process; the sub-process is revoked when the program ends. After the process is resident in the memory, Python only needs to start a sub-process once to perform any query. However, the trouble is that the get_user_id program needs to be rewritten: it should be changed to receive standard input in a loop, and the result should be sent to standard output. In addition, a special input (such as "EOF") is specified "), after receiving this input, the sub-process exits the loop. After get_user_id is changed


Int main (INT argc, char * ARGs [])
{Char name [512]; // buff
While (scanf ("% s", name) {If (! Strcmp (name, "EOF") return 0;
Printf ("% s \ n", GETID (name ));}
}

This C program first accepts the standard input and determines whether the input ends with the end mark. If yes, it returns; otherwise, the user ID is printed. How Should Python call this process? The pipe object obtained using popen () only supports write or read operations, but cannot read and write at the same time. You must allow both read and write operations. Use popen2 (). This function returns a tuple containing two elements. The two elements are the write pipeline (standard input can be written to the target process) and the read pipeline (standard output of the read process ). To deal with the above C program, open the pipeline pipe_out, pipe_in = popen2 ("get_user_id", "WR"); "WR" indicates that the next pipeline needs to be written and read. An example is as follows: Perform 10000 queries.

Import OS

If _ name _ = "_ main __":

Pipe_in, pipe_out = popen2 ("get_user_id", "WR ");
For Iin range (10000)
Pipe_in.write ("myname ");
Pipe_in.write ("\ n"); # A line break is required.
Pipe_in.flush (); # The buffer needs to be cleared.

Userid = pipe_out.readline (); # Read the result
In the above case, flush is generally required to prevent Io Buffering in order to smoothly interact with the process. Try to write and read the entire line as much as possible to control the pipeline interaction process. According to my experiment, the process of using resident memory for 10000 calls is much faster than that of non-resident memory. The former consumes about 1/5 of the time, however, this result is only a special case of the author. Analyze your application scenarios and see how to use the powerful Python pipeline tool. PS, Python, and popen3, which can be used to open stdin, stdout, and stderr of the process, which is better and more powerful. In addition, the subprocess module provides more powerful process management interfaces.

RP, WP, Ep = popen2.popen3 ("% s-e % S % s" % (addr2line_cmd, kernel_path, symbol_addr ))
Addr2error = ep. readlines ()
Addr2line = RP. Readline (). Strip ()
Ep. Close ()
RP. Close ()
WP. Close (

However, in the actual coding process, because popen separates various outputs, the OS. System (CMD) is used at last)
In addition, OS. startfile () is also said to be acceptable, but you have not tried it.

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.