Python Learning notes (15)

Source: Internet
Author: User

One, process and thread

An operating system can have multiple processes
A process can have multiple threads and must have one thread


II. Multi-process

From multiprocessing import Process
Import OS
# more functions to run
def run_proc (name):
Print ("Hello,", Name,os.getpid ())
if __name__ = = "__main__":
Print ("Parent process%s"% os.getpid ())
p = Process (Target=run_proc, args= ("Test",))
Print ("Chind process would start ...")
P.start () #调用进程执行指定的函数
P.join () #等待子进程结束后再继续往下执行, typically used for inter-process communication
Print ("Child process End ...")


Third, Process pool

When you want to create a large number of child processes, you can use a process pool to batch create child processes

From multiprocessing import Pool
Import OS
Import time
Import Random
# functions that define Process execution
def fun ():
Print ("--haha--")
Start_time = Time.time ()
Time.sleep (Random.random () * 4)
End_time = Time.time ()
Print ("Cost:%s", (End_time-start_time))
# Executing a process pool in the main thread
If __name__ = "__main__":
p = Pool (3)
# loop adds a process to the process pool
P.apply_async (fun,args= ())
# Close Process Pool
P.close ()
# Return to main process
P.join ()
Print ("End")


Iv. Sub-process

Child processes are often used to invoke third-party software or applications, and Python provides subprocess to support and implement child processes

Import subprocess

Subprocess.call () #父进程等待子进程完成, returns the exit message, which is the return code
Subprocess.check_call () #父进程等待子进程完成, returns 0, if not 0, error
Subprocess.check_out () #父进程等待子进程完成, returns the output of the child process to the standard output

When Subprocess.call ("Ls-l", Shell=true) #shell被设置为真, the command "ls-l" can be used directly instead of ["LS", "-l"]

You can use the Popen () class to get more personalized output and output, which objects that are generated by this class represent child processes
Popen () Creating an object The main program does not wait for the child process to complete, but by calling the child process's. Wait () method, the master program can wait for the child process to complete, which blocks the main process

You can also work with the child process in the parent process as follows:
Child.poll () #检查子进程状态
Child.kill () #终止子进程
Child.send_signal () #向子进程发送信号
Child.terminate () #终止子进程
The PID of the child process is stored in the Child.pid

By specifying the child process's: stdin = subprocess. Pipe,stdout = subprocess. Pipe,stderr = subprocess. Pipe can use the standard input and standard output of child processes in Python, subprocess provides a cache of standard input and standard output for child processes

You can use the communicate () method to enter the process for the child process
  
  


    

Python Learning notes (15)

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.