zoukankan      html  css  js  c++  java
  • 并行(多进程)-python

    1、进程创建

    2、当前进程信息

    使用current_process可获得当前进程的信息:

    (1)引入:from multiprocessing import current_process

    (2)获取信息:

    proc = current_process() # 当前进程对象
    print proc.name # 进程名字
    print proc.pid # 进程号

    3、进程状态及控制

    4、queue

     所属模块:multiprocessing.Queue

    (1)模块引入:from multiprocessing import Queue

    (2)API:

    q =  Queue()

    q.qsize():

    q.empty():

    q.full():

    q.put():

    q.put_nowait():

    q.get():

    q.get_nowait():

    q.close():

    join_thread():

    cancel_join_thread():

    4.1 简化版本

    所属模块:multiprocessing.queues.SimpleQueue

    只有三个方法:empty(),get(), put()

    4.2 加强版本

    所属模块:multiprocessing.JoinableQueue

    新增两个方法: task_done() ,join()

    5、pipe 

    6、锁

    7、进程间共享

     Pipe、Queue 都有一定数据共享的功能,但是他们会堵塞进程

    8、进程池

     

    9、注意事项

    (1)Lock、Pipe、Queue 和 Pipe 需要注意的是:尽量避免使用 Process.terminate 来终止程序,否则将会导致很多问题。

    (2)multiprocessing提供了threading包中没有的IPC(比如Pipe和Queue),效率上更高。应优先考虑Pipe和Queue,避免使用Lock/Event/Semaphore/Condition等同步方式 (因为它们占据的不是用户进程的资源)。

  • 相关阅读:
    Two Sum II
    Subarray Sum
    Intersection of Two Arrays
    Reorder List
    Convert Sorted List to Binary Search Tree
    Remove Duplicates from Sorted List II
    Partition List
    Linked List Cycle II
    Sort List
    struts2结果跳转和参数获取
  • 原文地址:https://www.cnblogs.com/qjoanven/p/8492386.html
Copyright © 2011-2022 走看看