Python multi-process multiprocessing module

Source: Internet
Author: User

Multiprocessing Common methods:
    • Cpu_count (): Count CPU Cores

      Multiprocessing.cpu_count ()

    • Active_children () gets all child processes

      Multiprocessing.active_children ()

    • Preces () Create a Process object

      Multiprocessing. Preces (Target=function_name, args= ())

      target: 函数名args: 函数需要的参数,以tuple形式传入,一个参数时需(1,)
Preces Common methods:
    • Is_alive () Determine if the process exists

    • Run () Start process

    • Start () starts the process and automatically calls the Run method, a common

    • Join ([timeout]) waits for the process to end or until it expires
      • Join () Method Description:
      def def worker(interval):    time.sleep(interval)    print(‘hello world‘)P = multiprocessing.Process(target=worker, args=(5,))#-----------------------------------P.start()#设置timeout 设置超时时间print(P.is_alive())P.join(timeout=3)print(‘end main‘)###    True    end main    hello world#-----------------------------------P.start()P.alive()# 不调置timeout 超时时间P.join()print()###    True    hello world    end main#-----------------------------------结论:当join()不设置timeout时程序会一直等待上面的进程执行完成后再执行join()后面的代码当设置timeout时,无论上面的进程是否执行完成,程序运行到指定时间后就会执行后面的代码
      Preces Common Properties
    • NAMD Process Name

    • The PID of PID process

Python multi-process multiprocessing module

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.