Python Web server Learning Notes (v) a probe into the popen principle of concurrent attempt

Source: Internet
Author: User

Can I use the new popen process to achieve concurrency? CGI handlers like this

  def run_cgi (self,handler):        cmd = "python" +handler.full_path        child_stdin,child_stdout=os.popen2 (cmd)        Child_stdin.close ()        data=child_stdout.read ()        child_stdout.close ()        handler.send_content (data)

I let it run.

For I in Range (1,100000): For    I in range (1,1000000):        

Apparently ran it to death, when running this CGI script, the server cannot process any get requests

So I wondered if it was because popen it was going to wait for the return value output, and then I made the following transformation

  def run_cgi (self,handler):        cmd = "python" +handler.full_path        child_stdin,child_stdout=os.popen2 (cmd)        CWD = "Python" + "/usr/fuckbitch.py"        stdin,stdout=os.popen2 (CWD)        child_stdin.close ()        data=child_ Stdout.read ()        child_stdout.close ()        handler.send_content (data)

"/usr/fuckbitch.py" is that can run 10 billion seconds of the dead loop, and then the server is not dead, it returned to the CMD page, but the dead loop of CWD run at the same time

You have to see the underlying principle, so use the PS command to see the process

Then use PS-T-P 26604 to view the threads under the process and discover

PID SPID TTY Time CMD

26604 26604 pts/0 00:00:18 python

While the other

PID SPID TTY Time CMD
26603 26603 pts/0 00:00:00 python <defunct>

Is this the one that ran for 18 seconds is my dead loop program, so this is actually a success?

So you can summarize the usage of popen. According to our experiment, Popen is not blocked. But when we need to wait for its return value, our program is blocked. And the popen pipes are of limited size. When the value of the output exceeds the pipe size of 65536, the Popen will be blocked, as in the previous experiment, I have changed the Fuckbitch to a dead loop.

For I in Range (1,1000000):    print "Fuck"

And wait for it to return. Obviously the 1s can run out on the usual server, but the server is still dead. The reason may be that the output is too much of a ghost animal (fog), after searching for the reason that the popen pipe is limited

Python Web server Learning Notes (v) a probe into the popen principle of concurrent attempt

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.