Process of Python multi-processes

Source: Internet
Author: User

    1. Because the fork creation process can no longer be used on Windows systems, multiprocessing is generated. Process
    2. Process can be instantiated directly and then called with start, you need to specify the function that the new process executes, and the arguments are passed in a tuple
    3. The join method of the process object blocks the main thread until the child process finishes executing, and the timeout parameter can specify the time-out
    4. Process implementation, the main process will not close until all child processes have finished executing
    5. Like the Java multithreading implementation, inherit the process class, rewrite the Run method, and then instantiate, and then drop the Start method
from multiprocessing import Processimport osimport timedef test(arg):    print(arg)    time.sleep(4)    print("the process %s is executing "%os.getpid())p1 = Process(target=test, args=("haha",))  # 以元组的形式传递参数p1.start()p1.join(timeout=2)  # 子进程执行结束之后主进程才继续往下执行# timeout设置超时时间 超过这个时间如果子进程还没结束 主进程将继续执行# p1.terminate()  # 杀死p1进程print("finish")  # 主进程执行完之后并不会关闭 而是会等子进程执行结束再关闭# 类似Java多线程  继承Process类  重写run方法 实例化 start调用class MyProcess(Process):    def run(self):        time.sleep(3)        print("haha...")m1 = MyProcess()m1.start()print("主进程执行到这里了....")

Process of Python multi-processes

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.